Skip to content

jquery_ref_traversing

jQuery provides a rich set of methods for navigating the Document Object Model (DOM) tree. Traversing allows you to find related elements based on their relationship (parent, child, sibling, etc.) to the currently selected element(s). Efficient traversal is key to concise and performant jQuery code.

MethodDescription
add()Adds elements to the current set of matched elements.
addBack()Adds the previous set of elements on the stack to the current set. Useful after a traversal method to include the starting element(s).
children([selector])Returns the direct children of each element in the set, optionally filtered by a selector.
closest(selector)Travels up the DOM tree from the current element(s) and returns the first ancestor that matches the selector.
contents()Returns the children of each element in the set, including text and comment nodes.
each(function)Executes a function for each element in the matched set. Not strictly traversal, but often used with traversed elements.
end()Ends the most recent filtering or traversing operation in the current chain, returning the set of elements to its previous state.
eq(index)Reduces the set of matched elements to the one at the specified index (zero-based). Negative index counts from the end.
filter(selector | function)Reduces the set of matched elements to those that match the selector or pass the function’s test.
find(selector)Returns descendant elements (children, grandchildren, etc.) of each element in the current set, filtered by a selector.
first()Reduces the set of matched elements to the first element in the set. Equivalent to .eq(0).
has(selector | element)Reduces the set of matched elements to those that have a descendant matching the selector or containing the given DOM element.
is(selector | function | element | jQuery object)Checks the current matched set against a selector, function, element, or jQuery object and returns true if at least one element matches.
last()Reduces the set of matched elements to the last element in the set. Equivalent to .eq(-1).
map(function)Passes each element in the current matched set through a function, producing a new jQuery object containing the return values.
next([selector])Returns the immediately following sibling of each element in the set, optionally filtered by a selector.
nextAll([selector])Returns all following siblings of each element in the set, optionally filtered by a selector.
nextUntil([selector | element], [filter])Returns all following siblings of each element up to, but not including, the element matched by the selector, DOM node, or jQuery object.
not(selector | function | element | jQuery object)Removes elements from the set of matched elements.
offsetParent()Returns the closest ancestor element that is positioned (CSS position other than ‘static’).
parent([selector])Returns the direct parent of each element in the set, optionally filtered by a selector.
parents([selector])Returns the ancestors (parent, grandparent, etc.) of each element in the set, optionally filtered by a selector.
parentsUntil([selector | element], [filter])Returns the ancestors of each element up to, but not including, the element matched by the selector, DOM node, or jQuery object.
prev([selector])Returns the immediately preceding sibling of each element in the set, optionally filtered by a selector.
prevAll([selector])Returns all preceding siblings of each element in the set, optionally filtered by a selector.
prevUntil([selector | element], [filter])Returns all preceding siblings of each element up to, but not including, the element matched by the selector, DOM node, or jQuery object.
siblings([selector])Returns the siblings (elements sharing the same parent) of each element in the set, optionally filtered by a selector.
slice(start, [end])Reduces the set of matched elements to a subset specified by a range of indices.

For detailed examples and further information, refer to the official jQuery Traversing documentation.