Reputation: 542
I mean, how can I find which of two elements belongs to a node nearest to 'document' or 'window'? which is higher in hierarchy?
EDIT this gets the job done:
function gerarchia(elem) {
var i=0;
while (elem.parentNode) {
elem = elem.parentNode;
i++;
}
return i;
}
Upvotes: 1
Views: 1413
Reputation: 19750
Try .parents().length
, like this:
if ( $(element1).parents().length > $(element2).parents().length ) {
// lower
}
else {
// higher
}
Upvotes: 1
Reputation: 78750
There may be a better way, but the brute force approach of just calling parentNode
repeatedly with a counter until you hit the root should work. Assuming your document isn't nested ridiculously deep, it should be relatively quick.
Upvotes: 1
Reputation: 34223
If you've got jQuery, you can just do this to find, for example, the first <li>
element:
$('li:first')
Upvotes: 0