DOM traversal methods
After creating a jQuery object using $()
, we can alter the set of matched elements we are working with by calling one of these DOM traversal methods. DOM traversal methods are discussed in detail in Chapter 2, Selecting Elements.
Filtering
Traversal method |
Returns a jQuery object containing… |
---|---|
|
Selected elements that match the given selector. |
|
Selected elements for which the callback function returns |
|
The selected element at the given 0-based index. |
|
The first selected element. |
|
The final selected element. |
|
Selected elements in the given range of 0-based indices. |
|
Selected elements that do not match the given selector. |
|
Selected elements that have a descendant matching |
Descendants
Traversal method |
Returns a jQuery object containing… |
---|---|
|
Descendant elements that match the selector. |
|
Child nodes (including text nodes). |
|
Child nodes, optionally filtered by a selector. |
Siblings
Traversal method |
Returns a jQuery object containing… |
---|---|
|
The sibling immediately following each selected element, optionally filtered by a selector. |
|
All siblings following each selected element, optionally filtered by a selector. |
|
All siblings following each selected element up to and not including the first element matching |
|
The sibling immediately preceding each selected element, optionally filtered by a selector. |
|
All siblings preceding each selected element, optionally filtered by a selector. |
|
All siblings preceding each selected element up to and not including the first element matching |
|
All siblings, optionally filtered by a selector. |
Ancestors
Traversal method |
Returns a jQuery object containing… |
---|---|
|
The parent of each selected element, optionally filtered by a selector. |
|
All ancestors, optionally filtered by a selector. |
|
All ancestors of each selected element up to and not including the first element matching |
|
The first element that matches the selector, starting at the selected element and moving up through its ancestors in the DOM tree. |
|
The positioned parent, either relative or absolute of the first selected element. |
Collection manipulation
Traversal method |
Returns a jQuery object containing… |
---|---|
|
The selected elements, plus any additional elements that match the given selector. |
|
The selected elements, plus the previous set of selected elements on the internal jQuery stack. |
|
The previous set of selected elements on the internal jQuery stack. |
|
The result of the callback function when called on each selected element. |
|
The specified elements. |
Working with selected elements
Traversal method |
Description |
---|---|
|
Determines whether any matched element is matched by the given selector expression. |
|
Gets the index of the matched element in relation to its siblings. |
|
Gets the index of the given DOM node within the set of matched elements. |
|
Determines whether DOM node |
|
Iterates over the matched elements, executing |
|
Gets the number of matched elements. |
|
Gets an array of DOM nodes corresponding to the matched elements. |
|
Gets the DOM node corresponding to the matched element at the given index. |
|
Gets an array of DOM nodes corresponding to the matched elements. |