jQuery Traversal

What is traversal?

jQuery traversal, meaning 'move', is used to 'find' (or select) HTML elements based on their relationship to other elements. Start with a selection and move along this selection until you reach the element you expect.

The following diagram shows a family tree. Through jQuery traversal, you can easily move up (ancestors), down (descendants), and horizontally (siblings) in the family tree starting from the selected (current) element. This kind of movement is called traversing the DOM.

Illustration:

Traverse the DOM tree
  • <div> Element is the parent of <ul> and also the ancestor of all its content.
  • <ul> Element is the parent of <li> and also a child of <div>.
  • The left <li> element is the parent of <span>, the child of <ul>, and also a descendant of <div>.
  • <span> Element is a child of <li> and also a descendant of <ul> and <div>.
  • The two <li> elements are siblings (they share the same parent element).
  • The right <li> element is the parent of <b>, the child of <ul>, and also a descendant of <div>.
  • <b> Element is a child of the right <li> and also a descendant of <ul> and <div>.

Tip:Ancestors are parent, grandparent, great-grandparent, and so on. Descendants are children, grandchildren, great-grandchildren, and so on. Siblings share the same parent.

Traverse DOM

jQuery provides various methods for traversing the DOM.

The largest category of traversal methods is tree traversal (tree-traversal).

The next chapter will explain how to move up, down, and horizontally in the DOM tree.

jQuery Traversal Reference Manual

For an overview of all jQuery traversal methods, please visit our jQuery Traversal Reference Manual.