How jQuery addBack Method Works in Chain Traversal
The jQuery addBack() method allows developers to include
the previous set of matched elements from the internal jQuery stack into
the current set during chained DOM traversal. Introduced in jQuery 1.8
to replace the deprecated andSelf() method, it simplifies
multi-level element manipulation by preventing the need to break chains
or re-select ancestral or sibling elements. This guide explains how the
jQuery internal stack manages elements, how addBack()
manipulates this chain, and how to use it effectively with optional
selector filters.
Understanding the jQuery Stack
When chaining traversal methods in jQuery, such as
.find(), .children(), or
.parent(), jQuery pushes each newly targeted set of
elements onto an internal stack. By default, subsequent operations only
affect the most recent set of elements at the top of that stack,
effectively leaving behind the elements targeted in previous steps of
the chain.
The addBack() method tells jQuery to reach one level
down into the stack, retrieve the previous set of matched elements, and
combine them with the current set.
Basic Syntax and Functionality
.addBack([selector])- selector (optional): A string containing a selector expression to filter which elements from the previous set are added to the current selection.
Without arguments, addBack() combines the entirety of
the previous selection with the current selection. If an optional
selector is provided, only elements from the previous selection that
match the selector will be merged into the current group.
How It Works in Practice
Consider the following HTML structure:
<div class="card">
<h2 class="title">Product 1</h2>
<p class="description">Item details go here.</p>
</div>If you want to apply a border to both the container
(.card) and its contents, you might traverse into the
children:
// Without addBack():
$('.card')
.children()
.addClass('highlight');
// Only <h2 class="title"> and <p class="description"> receive the class.By inserting .addBack(), the parent .card
is restored into the current selection alongside its children:
// With addBack():
$('.card')
.children()
.addBack()
.addClass('highlight');
// The <div class="card">, <h2>, and <p> elements all receive the class.Filtering with a Selector
The optional selector allows you to selectively restore elements from the previous state rather than combining all of them.
<ul class="menu primary">
<li class="item">Home</li>
<li class="item">About</li>
</ul>
<ul class="menu secondary">
<li class="item">Privacy</li>
</ul>$('ul')
.find('.item')
.addBack('.primary')
.addClass('active');In this case:
$('ul')selects both unordered lists..find('.item')narrows the selection to all threelielements..addBack('.primary')checks the previous stack (ulelements), matches onlyul.primary, and combines it with the current.itemselection..activeis applied to all threeliitems andul.primary, whileul.secondaryis excluded.
Contrast with end()
While addBack() merges the previous and current sets
together, the related end() method completely discards the
current set and rolls back to the previous set. Understanding the
difference is crucial for effective chain traversal:
.addBack(): Combines current elements + previous elements..end(): Discards current elements and reverts entirely to previous elements.