jQuery .addBack() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Selection stack

What You’ll Learn

The .addBack() traversing method merges the previous selection on jQuery’s internal stack with the current matched set — optionally filtered by a selector. This tutorial follows the official jQuery API demos for .nextAll() and .find(), compares .addBack() with .end(), and covers the deprecated .andSelf() rename.

01

Syntax

.addBack()

02

Stack

Prev + current

03

Filter

Optional sel

04

.end()

Not the same

05

find()

Parent + child

06

Since 1.8

Replaces andSelf

Introduction

Chained jQuery calls often narrow a selection: start with a list item, call .nextAll() to grab later siblings, or call .find("p") to descend into children. Sometimes you need both the original elements and the narrowed subset in one collection.

jQuery keeps an internal stack of matched sets as you traverse. The .addBack() method (added in jQuery 1.8, replacing .andSelf()) merges the previous stack entry with the current set so you can keep chaining — without starting a new $() query.

Understanding the .addBack() Method

After a traversing method pushes a new set onto the stack, .addBack() returns a jQuery object containing the union of the current set and the immediately previous set. Duplicates are removed; elements appear in document order when possible.

Pass an optional selector to filter the merged result — only elements matching that expression remain. This is handy when the union is broader than what you need but you still want stack-based merging instead of manual .add() calls.

💡
Beginner Tip

.end() goes back to the previous set. .addBack() keeps the current set and adds the previous one. Choose based on whether you need both groups or only the earlier selection.

📝 Syntax

General form of .addBack:

jQuery
.addBack( [selector ] )

Parameters

  • selector (optional) — a string containing a selector expression to filter the merged set. When omitted, every element from the previous and current sets is included.

Return value

  • A new jQuery object containing the previous stack set united with the current set, optionally filtered.

Official jQuery API example

jQuery
$( "li.third-item" ).nextAll().addBack()
  .css( "background-color", "red" );
// Items 3, 4, and 5 highlighted — not 1 or 2

⚡ Quick Reference

GoalCode
Merge with previous set.nextAll().addBack()
Container + descendants.find("p").addBack()
Filter after merge.addBack(".highlight")
Go back only (no merge).end()
Merge external elements.add(selector)
Legacy name (deprecated).andSelf() → use .addBack()

📋 .addBack() vs .end() vs .add()

Three ways to change which elements a chain targets.

.addBack()
prev + now

Union with previous stack set

.end()
prev only

Pop stack; discard current set

.add()
+ argument

Union with new matched elements

.andSelf()
deprecated

Old name; removed in modern jQuery

Examples Gallery

Examples 1–2 mirror the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official stack-merge patterns from the jQuery API.

Example 1 — Official Demo: nextAll() + addBack()

Select the third list item, get all following siblings, merge the starting item back in, and highlight items 3, 4, and 5 red.

jQuery
$( "li.third-item" ).nextAll().addBack()
  .css( "background-color", "red" );
Try It Yourself

How It Works

nextAll() alone selects items 4 and 5. The stack still remembers item 3. .addBack() unions both sets before .css() runs — the exact walkthrough in the jQuery API docs.

📈 Practical Patterns

Container + children, filtered merge, and stack comparison.

Example 2 — Official Demo: find() + addBack()

Inside a div, .find("p") selects only paragraphs. .addBack() brings the div itself into the set — both container and paragraphs get the class.

jQuery
// Only paragraphs highlighted:
$( "div.before-addback" ).find( "p" ).addClass( "background" );

// Div AND paragraphs highlighted:
$( "div.after-addback" ).find( "p" ).addBack().addClass( "background" );
Try It Yourself

How It Works

The official side-by-side demo shows why .addBack() matters after .find(): without it, the parent container is excluded from styling even when you want both wrapper and content highlighted.

Example 3 — Optional Selector Filter

Merge with the previous set, then keep only elements matching a class — .addBack(".active").

jQuery
$( "ul.menu" ).find( "li" ).addBack( ".active" )
  .css( "font-weight", "bold" );
Try It Yourself

How It Works

The selector argument filters after the union. Equivalent mental model: merge previous and current sets, then apply .filter(".active") in one step.

Example 4 — .addBack() vs .end()

Same starting chain, different stack behavior — merge vs revert.

jQuery
// addBack → items 3, 4, 5
$( "li.third-item" ).nextAll().addBack().length; // 3

// end → only item 3 (previous set)
$( "li.third-item" ).nextAll().end().length; // 1
Try It Yourself

How It Works

After nextAll(), the current set is items 4–5 and the stack holds item 3. .addBack() keeps all three; .end() discards 4–5 and returns only item 3.

Example 5 — Narrow, Merge, Then Style

Filter list items, merge the filtered subset with the list container via .addBack(), and apply one border.

jQuery
$( "ul.highlight-list" )
  .find( "li.done" )
  .addBack()
  .css( "border", "2px solid green" );
Try It Yourself

How It Works

.find("li.done") narrows to completed tasks. .addBack() pulls the ul back into the collection so the list wrapper and finished items share one visual treatment.

🚀 Common Use Cases

  • Sibling ranges — highlight a list item plus all following siblings with .nextAll().addBack().
  • Container styling — include a wrapper div when descendants are selected via .find().
  • Filtered unions — merge stack sets then restrict with .addBack(".class").
  • Event delegation cleanup — bind handlers on a parent and matched children in one chain.
  • Animation batches — fade a panel and its active child tabs together.
  • Legacy migration — replace deprecated .andSelf() calls with .addBack().

🧠 How .addBack() Uses the Stack

1

Initial selection

jQuery stores the first matched set on its internal stack.

Push
2

Traverse

.find(), .nextAll(), etc. push a new narrowed set.

Narrow
3

addBack()

Union previous stack entry with current set; optional selector filter.

Merge
4

Continue chain

Apply .css(), .on(), or other methods to the merged collection.

📝 Notes

  • Available since jQuery 1.8; replaces deprecated .andSelf() (jQuery 1.2–1.7).
  • Only merges with the immediately previous set on the stack — not the entire history.
  • Use .end() when you want the previous set alone, not a union.
  • Use .add() when merging with elements outside the stack (new selector or DOM node).
  • Optional selector filters the merged result — it does not change which sets are united.
  • Traversing methods that do not push a new set will leave nothing meaningful for .addBack() to merge.

Browser Support

.addBack() has been part of jQuery since 1.8+. It works wherever jQuery runs — all modern browsers and supported legacy IE versions with your chosen jQuery build.

jQuery 1.8+

jQuery .addBack()

Supported in jQuery 1.8+, 2.x, and 3.x. Replaces .andSelf() from older codebases. No native DOM equivalent — stack behavior is jQuery-specific.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.addBack() Universal

Bottom line: Safe in any jQuery 1.8+ project. Replace legacy .andSelf() calls when maintaining older plugins or themes.

Conclusion

The jQuery .addBack() method merges the previous selection on the traversal stack with the current matched set — optionally filtered by selector. It is the modern replacement for .andSelf() and the union counterpart to .end().

Reach for .addBack() after .find(), .nextAll(), or similar narrowing steps when you need both the original elements and the new subset in one chain.

💡 Best Practices

✅ Do

  • Use after .find() when the container must stay selected
  • Use .nextAll().addBack() for “this and following” ranges
  • Pass a selector to filter the merged union when needed
  • Replace legacy .andSelf() with .addBack()
  • Compare with .end() when only the previous set is needed

❌ Don’t

  • Confuse .addBack() with .add(selector)
  • Expect it to merge more than one stack level back
  • Call it when the prior step did not push a new set
  • Use .andSelf() in new jQuery 1.8+ code
  • Assume .end() and .addBack() are interchangeable

Key Takeaways

Knowledge Unlocked

Five things to remember about .addBack()

Merge the stack, keep chaining.

5
Core concepts
🗃 02

Prev + now

Union sets

Behavior
🔍 03

Selector

Optional filter

Arg
04

.end()

Not a merge

Compare
1.8 05

andSelf

Deprecated

History

❓ Frequently Asked Questions

.addBack() adds the previous set of elements on jQuery's internal traversal stack to the current set, returning a new jQuery object with both groups merged. An optional selector filters which elements from the combined result are kept.
.end() returns only the previous set on the stack — it replaces the current selection. .addBack() unions the previous set with the current set so you can style or bind events on both groups in one chain.
.add() merges the current set with new elements you pass as an argument (selector, DOM node, etc.). .addBack() merges with the immediately previous set already stored on jQuery's internal stack — no argument required unless you want a filter.
.andSelf() was the jQuery 1.2 name for the same behavior. It was deprecated in jQuery 1.8 and replaced by .addBack(). Use .addBack() in all modern jQuery code.
Use it after a narrowing step like .find(), .nextAll(), or .filter() when you need both the original matched elements and the new subset — for example, highlighting a list item plus everything after it, or a container div plus the paragraphs inside it.
.addBack(selector) merges the previous and current sets, then filters the union so only elements matching the selector remain. It is equivalent to merging first and then applying .filter(selector).
Did you know?

jQuery renamed .andSelf() to .addBack() in version 1.8 to clarify that the method adds the previous set back into the current collection — not an unrelated “self” reference. The behavior is identical; only the name changed to read more clearly in long chains like .find("p").addBack().css(...).

Continue to jQuery .andSelf()

Learn the legacy stack-merge method and how to migrate it to .addBack().

.andSelf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful