Hi Pete,
Yes, Sam's selector will definitely stop climbing the DOM once it
finds the first match. That's what the :eq(0) part does.
The :first selector does the same thing as :eq(0), so this would work
equally as well:
$("#myelement").parents("table:first")
--Karl
____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Jul 18, 2008, at 2:51 AM, ProggerPete wrote:
Mmm tasty. That does look the goods. Do you know whether under the
hood it is smart enough to stop climbing the DOM once it finds the 1st
match?
Cheers,
Pete
On Jul 17, 5:40 pm, Sam Collett <[EMAIL PROTECTED]> wrote:
How about:
$("#myelement").parents("table:eq(0)")
Does that do what you want?
Example:http://demos.texotela.co.uk/parents.html
--Sam
On Jul 17, 7:51 am, ProggerPete <[EMAIL PROTECTED]> wrote:
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:jquery-
[EMAIL PROTECTED] On
Behalf Of ProggerPete
Sent: Tuesday, July 15, 2008 10:41 AM
To: jQuery (English)
Subject: [jQuery]MyfirstjQueryplugin
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
onmyimplementation.
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 thefirstparent 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