Best way to find out if element is a descendant of another
JqueryJquery Problem Overview
I am in the process of implementing jQuery, and taking out Prototype libraries in my codebase, and I am wondering if you could give me the best way to implement this functionality in jQuery.
I am familiar with the jQuery ancestor>
descendant syntax, but just want to check if an element is a descendant by true of false, like the code below:
can someone give me the most efficient jQuery solution for this ?
<div id="australopithecus">
<div id="homo-herectus">
<div id="homo-sapiens"></div>
</div>
</div>
$('homo-sapiens').descendantOf('australopithecus');
// -> true
$('homo-herectus').descendantOf('homo-sapiens');
// -> false
Jquery Solutions
Solution 1 - Jquery
In jQuery 1.6, you can use the following code generically, e.g. targetElt and parentElt can both be DOM elements or jQuery-wrapped objects, as well as selectors:
$(targetElt).closest(parentElt).length > 0
Some of the other answers require you to refer to elements by their IDs, which isn't useful if all you have is a DOM element without an ID. Also, if you want to make sure that the targetElt is a strict descendant of parentElt (in other words, you don't want to count parentElt as its own descendant), make sure to add a targetElt != parentElt
check before your call to .closest()
, or use .parents().find()
as Jonathan Sampson suggests.
Solution 2 - Jquery
JQuery
With jQuery >=1.4 (2010) you can use the very fast function jQuery.contains()
This static method works with DOM elements, not with jQuery elements and returns true
or false
.
jQuery.contains( container, descendant )
Example: To check if a element is in the document you could do this:
jQuery.contains( document.body, myElement )
Native DOM
There is also a native DOM method Node.contains() that all browsers since ie5+ supports. So you can do it without jQuery:
document.body.contains( myElement )
Solution 3 - Jquery
I would think you could take advantage of CSS style selection here, with returned length..
$('#australopithecus #homo-sapiens').length // Should be 1
$('#homo-sapiens #homo-herectus').length // Should be 0
Not exactly true/false, but checking 0/1 as a boolean should work. :)
Alternately, you could do something like $('#parent').find('#child') and check the length there.
Solution 4 - Jquery
How about
$("#homo-herectus").parents().is("#australopithecus");
Solution 5 - Jquery
You can use the is()
function like so:
alert($('#homo-sapiens').is('#australopithecus *'));
// -> true
alert($('#homo-herectus').is('#homo-sapiens *'));
// -> false
Solution 6 - Jquery
$.fn.descendantOf = function(element) {
element = $(element)[0];
var current = this;
var body = document.body;
while (current && current != element && current != document.body) {
current = $(current).parent()[0];
}
if (typeof(current) == "undefined" || typeof(current) == "null") {
return false;
} else if (current == element) {
return true;
} else if (current == document.body) {
return false;
}
}
Example:
<div id="foo">
<div id="bar">
<div id="baz"></div>
</div>
</div>
And:
$('#foo').descendantOf('#bar'); // false
$('#foo').descendantOf('#foo'); // false
$('#foo').descendantOf(document.body); // true
$('#bar').descendantOf('#foo'); // true
$('#baz').descendantOf('#foo'); // true
Solution 7 - Jquery
You could attempt to .find()
it in the Elements .children()
$("#lucy").find("#homo-erectus").length;
Or the opposite direction:
$("#homo-erectus").parents().find("#lucy").length;
Solution 8 - Jquery
Best method that I found is using Dan G. Switzer, II's method found here: http://blog.pengoworks.com/index.cfm/2008/9/24/Using-jQuery-to-determine-if-an-element-is-a-child-of-another-element
jQuery.fn.isChildOf = function(b){
return (this.parents(b).length > 0);
};
Then you would just use the plugin as:
$('homo-sapiens').isChildOf('australopithecus');
// -> true
$('homo-herectus').isChildOf('homo-sapiens');
// -> false
Solution 9 - Jquery
An alternative to closest() that uses (almost) the same traverse principle and doesn't include the element itself: child.parentsUntil(ancestor).last().parent().is(ancestor)
.
var child = $('#homo-sapiens');
var ancestor = $('#australopithecus');
console.log(child.parentsUntil(ancestor).last().parent().is(ancestor)); // true
Solution 10 - Jquery
function descendantOf(parentId, childId) {
return ( $('#'+parentId+' > #'+childId).length === 1 );
}
That should work.
As was pointed out in the comment below, if you don't want it to be just direct descendants:
function descendantOf(parentId, childId) {
return ( $('#'+childId, $('#'+parentId)).length === 1 );
}
Solution 11 - Jquery
Supposing to rewrite your initial statement in:
$('#homo-sapiens').descendantOf('#australopithecus');
try to plugin:
(function($) {
$.fn.descendantOf = function(parentId) {
return this.closest(parentId).length != 0;
}
})(jQuery)