⊗jqSeAPS 83 of 113 menu

Finding All Parents in jQuery

The parent method gets the immediate parent of an element. To get all the parents of an element - not only the immediate one, but also the ancestor, great-grandparent, and so on up to the root element, use the parents method.

In the following example we will consider the following HTML code:

<div> <div class="www"> <div class="www"> <p id="test">text</p> </div> </div> </div>

Then we find the element #test and all its parents using parents and if they are parents of div with class www then we prepend the text '!' to them:

$('#test').parents('div.www').prepend('!');

Find ALL parents of tags b, and color those with class test red.

byenru