jQuery .addBack() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .addBack()?

.addBack([selector]) (since jQuery 1.8) merges the previous set on jQuery’s traversal stack with the current matched set. Optional selector filters the combined result. Traversal methods push a new set; .addBack() brings the prior one back.

Remember
$("li.third-item").nextAll().addBack()   // items 3, 4, 5 (official)
$("div").find("p").addBack()             // div + its paragraphs
$("div").find("p").addBack(".panel")     // union, then keep .panel

// end()   → previous set only (replace)
// addBack() → previous + current (union)
// andSelf() → old name — use addBack()
NeedUse
Previous + current.addBack()
Previous only.end()
Merge by argument.add(…)
Filter after merge.addBack(selector)
Legacy name.andSelf() → migrate to .addBack()

How to Use .addBack()

Narrow the set, then pull the previous stack entry back in when you need both.

  1. Start with a selection (e.g. $("li.third-item") or $("div.panel")).
  2. Traverse with .nextAll(), .find(), .children(), etc. (pushes a new set).
  3. Call .addBack() to union previous + current.
  4. Optionally pass a selector: .addBack(".keep").
  5. Chain styling or other methods on the combined set.
PieceRequired?Role
Prior traversalYesSomething that pushed a new stack set
.addBack()YesMerge previous stack entry with current
selectorOptionalFilter the union
Return value—New jQuery set (document order)

Pseudocode

Pseudocode
call $(set).nextAll()   // or .find() / .children() …
  // stack: [original, narrowed]

call .addBack([selector])

result = union(previous stack set, current set)
if selector provided:
    result = result.filter(selector)
sort result in document order
return result as a new jQuery object

Cheat Sheet

Stack merge vs replace vs argument merge.

TopicDetail
Syntax.addBack([selector])
SincejQuery 1.8
vs .end()Union vs replace with previous
vs .add()Stack previous vs pass new nodes
vs .andSelf()Same idea; use .addBack()
Official list demo$("li.third-item").nextAll().addBack()
DOM change?No — collection only

Live Preview

Compare .nextAll() alone, with .addBack(), and with .end().

  • list item 1
  • list item 2
  • list item 3
  • list item 4
  • list item 5

Official idea: nextAll + addBack keeps item 3 plus items 4 and 5.

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

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

Keep the starting item and everything after it (jQuery API demo).

jQuery
$("li.third-item").nextAll().addBack().append(" [kept]");
Try It Yourself

How It Works

  1. Start. Select the third list item.
  2. Narrow. .nextAll() keeps only items 4 and 5.
  3. Merge. .addBack() brings item 3 back into the set.
  4. Result. Items 3–5 are marked together.

Example 2 — Official: find("p") + addBack()

Without .addBack(), only paragraphs match; with it, the parent joins too.

jQuery
// Without addBack — paragraphs only:
$("div.before").find("p").append(" [p]");

// With addBack — div + paragraphs:
$("div.after").find("p").addBack().append(" [both]");
Try It Yourself

How It Works

  1. find. Moves from the div down to its paragraphs (new stack set).
  2. Without. Only those paragraphs remain selected.
  3. addBack. Unions the div back with its paragraphs.
  4. Use. Style a container and its children in one chain.

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

Union keeps both groups; .end() drops the narrowed set.

jQuery
var a = $("li.third-item").nextAll().addBack().length; // 3
var b = $("li.third-item").nextAll().end().length;     // 1
$("#out").text("addBack → " + a + "\\nend → " + b);
Try It Yourself

How It Works

  1. Same start. Both chains begin at item 3, then call .nextAll().
  2. addBack. Merges item 3 with 4 and 5 → length 3.
  3. end. Pops back to only item 3 → length 1.
  4. Pick. Need both groups? addBack. Need only the earlier set? end.

Example 4 — Optional Selector: .addBack(".keep")

Merge, then keep only elements that match the filter.

jQuery
$("li.start").nextAll().addBack(".keep").append(" [kept]");
Try It Yourself

How It Works

  1. Traverse. .nextAll() builds the narrowed set.
  2. Union. .addBack() would include the start item too.
  3. Filter. The selector keeps only .keep elements from that union.
  4. Same idea. Like merge first, then .filter(".keep").

Example 5 — .addBack() vs .add()

Stack previous set vs pass a new selector yourself.

jQuery
// Stack: previous selection comes from .find()
$("#a").find("p").addBack().append(" [stack]");

// Argument: you name the extra node yourself
$("#b").find("p").add("#b").append(" [arg]");
Try It Yourself

How It Works

  1. addBack. Pulls the previous stack entry (#a) automatically.
  2. add. You must pass "#b" (or a node) yourself.
  3. Same goal. Often the same final set after .find().
  4. Prefer. Use .addBack() when the previous set is already on the stack.

Edge Cases & Pitfalls

Stack mistakes that empty or surprise the set.

stack

Needs a prior traversal

Calling .addBack() with nothing on the stack to restore does not help. Use it after .find(), .nextAll(), etc.

end

Not the same as .end()

.end() discards the current set. .addBack() keeps both. Pick the wrong one and you lose nodes.

legacy

.andSelf() breaks on jQuery 3

Replace every .andSelf() with .addBack() before upgrading — the old method was removed.

filter

Selector filters the union

.addBack(".x") can drop the previous set if those elements do not match .x.

Browser Support

.addBack() has been part of jQuery since 1.8. It works in every browser that runs your loaded jQuery version (1.8+, 2.x, 3.x). Prefer it over legacy .andSelf().

✓ Stable · jQuery 1.8+

jQuery .addBack()

Supported across jQuery 1.8+, 2.x, and 3.x. Replaces .andSelf() from older codebases. Stack behavior is jQuery-specific.

100% With jQuery 1.8+
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() when migrating to jQuery 3.

Performance Notes

ApproachCostBest for
.addBack()CheapReuse the previous stack set
.addBack(sel)OKUnion + one filter pass
.add(sel) insteadMay re-queryWhen previous set is not on the stack
Narrow start setFasterPrefer scoped roots before .find()

Prefer .addBack() after a traversal when the previous set is already known — it avoids selecting those elements again with a second query.

Key Takeaways

  • Role: union previous stack set with the current set.
  • vs end: keep both vs restore previous only.
  • Legacy: replace .andSelf() with .addBack().
  • Next: read .andSelf() only for legacy migration.

One line: after you narrow a set, .addBack() brings the previous selection back into the chain.

Frequently Asked Questions

.addBack() adds the previous set of elements on jQuery's internal traversal stack to the current set. Optionally pass a selector to keep only matching elements from that union. Available since jQuery 1.8.
.end() returns only the previous set — it replaces the current selection. .addBack() unions previous + current so both groups stay in one set for chaining.
.add() merges elements you pass as an argument (selector, DOM node, HTML, or jQuery object). .addBack() merges with the previous stack entry — no argument required unless you want a filter.
.andSelf() was the old name (jQuery 1.2). Deprecated in 1.8 when .addBack() was introduced, and removed in jQuery 3.0. Use .addBack() in all modern code.
It merges previous and current sets, then filters the result so only elements matching the selector remain — like merging first, then .filter(selector).
After a narrowing step such as .find(), .nextAll(), or .children() when you still need the earlier elements too — for example a list item plus everything after it, or a div plus its paragraphs.

Next: .andSelf() Method

Read the deprecated alias 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.

9 people found this page helpful