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.
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 (for example, relative, absolute) of the first selected element |
Collection manipulation
Traversal Method |
Returns a jQuery object containing |
---|---|
|
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 |
---|---|
|
Determine whether any matched element is matched by the given selector expression |
|
Get the index of the matched element in relation to its siblings |
|
Get the index of the given DOM node within the set of matched elements |
|
Determine whether DOM node |
|
Iterate over the matched elements, executing |
|
Get the number of matched elements |
|
Get an array of DOM nodes corresponding to the matched elements |
|
Get the DOM node corresponding to the matched element at the given index |
|
Get an array of DOM nodes corresponding to the matched elements |