Hi Andy, parents doesn't do quite what I want. I want a method I can use in event delegation handlers that basically says, 'Give me the nearest x element'. I could of course check the current element, if it doesn't match I could then do a parents and then work on the 1st result in that, but that would be a bunch of extra processing for no gain (while it continues to check parents that I have no interest in).
I was more after info about any silly things I've done while implementing the function. I've since discovered pushStack which helps a fair bit. =) Cheers, Pete On Jul 15, 6:19 pm, "Andy Matthews" <[EMAIL PROTECTED]> wrote: > Pete... > > There is a built in parent method which does pretty much what you're doing. > > http://remysharp.com/jquery-api/ > > Look under parents(). > > -----Original Message----- > From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On > > Behalf Of ProggerPete > Sent: Tuesday, July 15, 2008 10:41 AM > To: jQuery (English) > Subject: [jQuery] My first jQuery plugin > > Hi folks, > I'm very new to jQuery and am playing around with what is possible. > > I found myself wanting a findParent function and couldn't spot one ready > made. I'd love it if people could give me feedback on my implementation. > I'm sure there's plenty I could do better. If this function also exists I'd > like to know where it is too. =) > > /** > * return the first parent that matches the selector. Optionally include > the src element in the search */ $.fn.findParent = function() { > var _this, _selector, _includeSelf, _result; > > function find(index, elem) > { > if (!_includeSelf) elem = elem.parentNode; > while (elem) > { > if ($(elem).is(_selector)) return > _result.push(elem); > elem = elem.parentNode; > } > } > > function _findParent(selector, includeSelf) > { > var thisp = _this = this; > _selector = selector; > _includeSelf = includeSelf; > _result = []; > this.each(find); > var retObj = $(_result); > retObj.end = function () { return thisp }; > return retObj; > } > > return _findParent; > }(); > > Cheers, > Pete