.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
Call
What 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
Parameter
Description
Step 1
Select a starting set (e.g. $("li.third-item")).
Step 2
Traverse with .nextAll(), .find(), etc. (pushes a new stack set).
Step 3
Call .andSelf() to union previous + current (jQuery 1.2–2.x).
Return Value
Return Value
Description
See examples
Check 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).
div.panel [both]
First Paragraph [both]
Second Paragraph [both]
How It Works
find. Moves from the panel to its paragraphs.
andSelf. Unions the panel back into the set.
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");
div.panel [new]
paragraphs [new]
(Runs on jQuery 3 via .addBack())
How It Works
Find. Search the codebase for .andSelf(.
Replace. Change each call to .addBack().
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.