jQuery .andSelf() Method

Beginner
5 min read
Updated: Oct 2026
5 examples
Legacy API

What Is .andSelf()?

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

Remember
// 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
NeedUse
New / jQuery 3 code.addBack()
Read old code.andSelf() ≈ .addBack()
Previous set only.end()
Filter after merge.addBack(selector) (not .andSelf())
jQuery 3 upgradeReplace every .andSelf()

How to Use .andSelf() (Legacy)

Same chaining pattern as .addBack() — then rename for modern jQuery.

  1. Select a starting set (e.g. $("li.third-item")).
  2. Traverse with .nextAll(), .find(), etc. (pushes a new stack set).
  3. Call .andSelf() to union previous + current (jQuery 1.2–2.x).
  4. Migrate: change .andSelf() → .addBack().
  5. On jQuery 3, never call .andSelf() — it throws.
PieceRequired?Role
Prior traversalYesSomething that pushed a new stack set
.andSelf()Yes (legacy)Merge previous + current (no args)
jQuery version1.2–2.xMissing in jQuery 3.0+
Modern replacement—.addBack() (1.8+)

Pseudocode

Pseudocode
// Legacy:
call $(set).nextAll().andSelf()
  → same as .addBack() with no selector

// Migration:
replace ".andSelf()" with ".addBack()"
if you need a filter:
  use .addBack(selector)   // andSelf never supported this

// jQuery 3:
if .andSelf is undefined → TypeError

Cheat Sheet

Keep the rename and version cutoffs clear.

TopicDetail
Syntax.andSelf() — no arguments
AddedjQuery 1.2
DeprecatedjQuery 1.8
RemovedjQuery 3.0
Replacement.addBack() (identical no-arg form)
vs .end()Union vs previous-only
Selector filter?No — use .addBack(sel)

Live Preview

This demo loads jQuery 2.2.4 so .andSelf() still exists. Compare it with .addBack() and .end().

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

On jQuery 2.x, .andSelf() and .addBack() both keep items 3–5. On jQuery 3, only .addBack() works.

Result
Ready — choose a call and click Run.

jQuery Examples

Legacy snippets use jQuery 2.2.4 so .andSelf() runs. Migration labs use jQuery 3.7.1 with .addBack().

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.
  4. Today. Write .addBack() for the same steps.

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.
  4. Migrate. Swap to .addBack() for jQuery 3.

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.
  4. Bonus. Use .addBack(sel) if you need a filter.

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

Union keeps both groups; .end() restores the previous set only.

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

How It Works

  1. Same start. Both chains use item 3, then .nextAll().
  2. andSelf. Merges item 3 with 4 and 5 → length 3.
  3. end. Pops back to only item 3 → length 1.
  4. Modern. Prefer .addBack() for the union case.

Example 5 — jQuery 3: .andSelf Is Missing

Detect removal and fall back to .addBack().

jQuery
var msg = typeof jQuery.fn.andSelf === "function"
  ? ".andSelf exists (jQuery 1.2–2.x)"
  : ".andSelf missing — use .addBack()";
$("#out").text(msg);

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

How It Works

  1. Check. jQuery.fn.andSelf is undefined on jQuery 3.
  2. Avoid call. Calling it would throw a TypeError.
  3. Use addBack. The modern method is always present on 1.8+ / 3.x.
  4. Upgrade. Fix source before shipping jQuery 3.

Edge Cases & Pitfalls

Where legacy code breaks during upgrades.

jquery 3

.andSelf is not a function

Removed in 3.0. Search the project for .andSelf( before upgrading.

name

Not “current element self”

It merges the previous stack set. That confusion is why jQuery renamed it to .addBack().

filter

No selector argument

.andSelf() never accepted a filter. Use .addBack(selector) after migration.

migrate

jQuery Migrate is temporary

Migrate may restore .andSelf() during upgrades — still replace it in source.

Browser Support

.andSelf() existed in jQuery 1.2 through 2.x. It was deprecated in 1.8 and removed in 3.0. Use .addBack() (jQuery 1.8+) everywhere — including all jQuery 3.x builds.

✓ Deprecated · removed in 3.0

jQuery .andSelf()

Works only on jQuery 1.2–2.x. Replacement .addBack() works on jQuery 1.8+ and all 3.x browsers that run jQuery.

Legacy Use .addBack() instead
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
.andSelf() Removed

Bottom line: Read .andSelf() in old code; write .addBack() in all new and migrated projects. Plan a find-and-replace before jQuery 3.

Performance Notes

ApproachCostBest for
.addBack()Same as andSelfAll modern code
.andSelf()Same (legacy)jQuery 1.2–2.x only
Stay on jQuery 2 for andSelfRiskAvoid — migrate instead

There is no performance reason to keep .andSelf(). The cost matches .addBack(); the risk is a hard break on jQuery 3.

Key Takeaways

  • Role: legacy stack union — same as .addBack().
  • Status: deprecated 1.8, removed 3.0.
  • Fix: replace with .addBack().
  • Next: .children() — direct child elements.

One line: read .andSelf() as .addBack() — and never ship the old name on jQuery 3.

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: .children() Method

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

.children() 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