The parent method in jQuery
We've already covered the parent method a bit when we discussed the parent pseudo-class. Let me remind you that the parent method and the parent selector do different things: the first finds the parent of an element, and the second finds all non-empty elements (that are parents).
In the following example, we will find the element #test, then find its parent with parent and set the text '!' to it. Let's say we have the following HTML code:
<p>outside</p>
<div>
<p>inside</p>
<p>inside</p>
<p id="test">inside</p>
<p>inside</p>
<p>inside</p>
</div>
<p>outside</p>
Now we write the parent method into the Javascript code:
$('#test').parent().html('!');
Don't forget that you can pass a selector to this method for additional filtering of parents.
Find the parent of the element with class www and color it red.
Find the parents of all b tags and make them red.
Find the parents of all tags b, and color those with class test red.