jQuery .addBack() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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()
CallWhat it does
Prior traversalSomething that pushed a new stack set
.addBack()Merge previous stack entry with current
selectorFilter the union
Return valueNew jQuery set (document order)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with a selection (e.g. $("li.third-item") or $("div.panel")).
Step 2Traverse with .nextAll(), .find(), .children(), etc. (pushes a new set).
Step 3Call .addBack() to union previous + current.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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 — Deprecated (Use .addBack())

Read the deprecated alias and how to migrate it to .addBack().

.andSelf() Method — Deprecated (Use .addBack()) tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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