jQuery "Traversal"
What is Traversal?
jQuery traversal, meaning "move," is used to "find" (or select) HTML elements based on their relationship to other elements. Starting with a selection, you move along this selection until you reach the desired element.
The diagram below shows a family tree. With jQuery traversal, you can easily move up (ancestors), down (descendants), and horizontally (siblings) in the family tree, starting from the selected (current) element. This movement is referred to as traversing the DOM.
Diagram Explanation:
- The <div> element is the parent of <ul> and an ancestor of all its content.
- The <ul> element is the parent of <li> elements and a child of <div>.
- The left <li> element is the parent of <span>, a child of <ul>, and a descendant of <div>.
- The <span> element is a child of <li> and a descendant of <ul> and <div>.
- The two <li> elements are siblings (having the same parent).
- The right <li> element is the parent of <b>, a child of <ul>, and a descendant of <div>.
- The <b> element is a child of the right <li> and a descendant of <ul> and <div>.
| | Ancestors are parent, grandparent, great-grandparent, etc. Descendants are child, grandchild, great-grandchild, etc. Siblings have the same parent. | | --- | --- |
Traversing the DOM
jQuery provides various methods for traversing the DOM.
The largest category of traversal methods is tree traversal.
The next chapter will explain how to move up, down, and sideways in the DOM tree.