jQuery .andSelf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.andSelf() (jQuery 1.2–2.x) merges the previous stack set with the current set — same idea as .addBack() with no selector. It takes no arguments. Learn it to read legacy code; write .addBack() in new work.

Syntax

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

jQuery
// Legacy (jQuery 1.2–2.x only):
$("li.third-item").nextAll().andSelf()

// Modern (jQuery 1.8+ / 3.x):
$("li.third-item").nextAll().addBack()

// Same union — rename only
// andSelf() never had a selector filter
// addBack(selector) can filter the union
CallWhat it does
.andSelf()Learn jQuery .andSelf(): legacy stack merge identical to .addBack(), deprecated in 1.8 and removed in 3.0, migration steps, vs .end(), examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select a starting set (e.g. $("li.third-item")).
Step 2Traverse with .nextAll(), .find(), etc. (pushes a new stack set).
Step 3Call .andSelf() to union previous + current (jQuery 1.2–2.x).

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Legacy: nextAll() + andSelf()

Same result as the modern .addBack() list demo (jQuery 2.x).

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

How It Works

  1. Start. Select the third list item.
  2. Narrow. .nextAll() keeps items 4 and 5.
  3. Merge. .andSelf() brings item 3 back.

Example 2 — Legacy: find("p") + andSelf()

Include the parent div with its paragraphs (classic plugin pattern).

jQuery
$("div.panel").find("p").andSelf().append(" [both]");
Try It Yourself

How It Works

  1. find. Moves from the panel to its paragraphs.
  2. andSelf. Unions the panel back into the set.
  3. Mark. Parent and children get the same suffix.

Example 3 — Migrate .andSelf() → .addBack()

One-to-one rename; optional filter only exists on .addBack().

jQuery
// Before (breaks on jQuery 3):
// $("div.panel").find("p").andSelf().append(" [old]");

// After:
$("div.panel").find("p").addBack().append(" [new]");

// Need a filter? andSelf never could — use:
// $("div.panel").find("p").addBack(".panel");
Try It Yourself

How It Works

  1. Find. Search the codebase for .andSelf(.
  2. Replace. Change each call to .addBack().
  3. Same union. No-arg behavior matches the old method.

Frequently Asked Questions

.andSelf() adds the previous set of elements on jQuery's internal traversal stack to the current set — the same union as .addBack() with no selector. It takes no arguments. Added in jQuery 1.2.
Yes for the no-argument form. jQuery renamed .andSelf() to .addBack() in 1.8. .addBack() can also take an optional selector filter; .andSelf() never could.
Deprecated in jQuery 1.8 because the name was confusing. Removed in jQuery 3.0. Official docs: use .addBack() instead, which should work identically.
No. Calling it throws because the method no longer exists. Replace every .andSelf() with .addBack() before upgrading.
Find-and-replace .andSelf() with .addBack(). For a filtered union, use .addBack(selector) — something .andSelf() never supported.
Legacy themes, old plugins, and older Stack Overflow answers. Read it as .addBack() when migrating to jQuery 3.

Next: .end() Method — Restore Previous Selection

Select only the direct children of each element in the set.

.end() Method — Restore Previous Selection 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