jQuery Tutorials
jQuery Effects
jQuery HTML
jQuery Traversing
jQuery AJAX
jQuery Misc
With jQuery you can skip the DOM tree to get the ancestors of something.
An ancestor is a parent, grandfather, ancestor, and so on.
Three useful jQuery methods for traversing up the DOM tree are:
parent()
parents()
parentsUntil()
The parent()
method returns the direct parent component of the selected element.
This route exceeds only one level at the top of the DOM tree.
The following example returns the direct parent component of each element <span>
:
Parents()
method returns all the ancestral features of the selected object, down to the root of the document (<html>
).
The following example returns all the ancestors of all aspects of <span>
:
You can also use the optional parameter to filter your ancestor search.
The following example returns all the ancestors of all <span> elements <ul>:
parentsUntil()
returns all ancestral traits between two given arguments.
The following example returns all the ancestral features between a <span>
and <div>
element: