The .andSelf() method was jQuery’s original name for merging the previous stack selection with the current set. It behaves like .addBack() with no arguments. This tutorial explains legacy syntax for reading old code, shows classic .nextAll() and .find() patterns, and guides migration to .addBack() in jQuery 3.x.
01
Syntax
.andSelf()
02
Stack
Prev + current
03
Since 1.2
Legacy API
04
Deprecated
jQuery 1.8
05
Removed
jQuery 3.0
06
addBack
Replacement
Fundamentals
Introduction
jQuery introduced .andSelf() in version 1.2 to solve a common chaining problem: after .find() or .nextAll() narrows a selection, developers often needed the original elements back in the same collection — without starting a new query.
The method merges the previous matched set on jQuery’s internal stack with the current set. In jQuery 1.8 it was deprecated and renamed to .addBack(). In jQuery 3.0 it was removed entirely. You will still encounter .andSelf() in legacy themes and plugins — this page teaches what it does and how to upgrade.
Concept
Understanding the .andSelf() Method
.andSelf() takes no arguments and returns a jQuery object containing the union of the current matched elements and the immediately previous set on the traversal stack. Duplicates are removed; results appear in document order when all nodes share one document.
The official jQuery API states it should work identically to .addBack() without a selector. The only modern difference is that .addBack(selector) can filter the merged union — a capability .andSelf() never had.
⚠
Migration Alert
Do not use .andSelf() in new code. Use .addBack(). If you load jQuery 3.x, .andSelf is undefined and chains will break.
Foundation
📝 Syntax
General form of .andSelf:
jQuery
.andSelf()
Parameters
None — this method does not accept any arguments.
Return value
A new jQuery object with the previous stack set united with the current set.
Without andSelf: only p elements highlighted.
With andSelf / addBack: div.panel and its p children highlighted.
How It Works
This is the legacy version of the official .addBack() side-by-side demo. Old WordPress jQuery snippets often use .andSelf() after .find() for exactly this reason.
Example 3 — Migrate .andSelf() to .addBack()
A straightforward find-and-replace when upgrading from jQuery 2.x to 3.x.
jQuery
// Before (breaks on jQuery 3):
$( ".menu" ).find( "li.active" ).andSelf().slideDown();
// After (jQuery 1.8+ through 3.x):
$( ".menu" ).find( "li.active" ).addBack().slideDown();
// Need a filter? andSelf never supported this — use addBack:
$( ".menu" ).find( "li" ).addBack( ".menu" ).addClass( "open" );
addBack() chain runs on jQuery 3.7+
andSelf() would throw: $(...).andSelf is not a function
How It Works
Search your codebase for .andSelf( before any jQuery 3 upgrade. Each occurrence is a one-line fix unless you also need selector filtering — then use .addBack(selector).
Example 4 — .andSelf() vs .end() (Legacy)
Same stack, different outcome — merge versus revert.
jQuery
// andSelf → 3 elements (items 3, 4, 5)
$( "li.third-item" ).nextAll().andSelf().length;
// end → 1 element (item 3 only)
$( "li.third-item" ).nextAll().end().length;
Legacy codebases used .andSelf() when both groups were needed and .end() when only the earlier selection mattered. That distinction still applies — only the merge method was renamed.
Example 5 — Reading a Legacy Plugin Snippet
Recognize .andSelf() in old jQuery plugin code and translate it mentally to .addBack().
Both versions add "section-active" to the plugin root AND .content descendants.
How It Works
Plugin authors chained .find().andSelf() to include the container element in the same class toggle as its contents. Updating the plugin for jQuery 3 is a single-method rename.
Applications
🚀 Common Use Cases
Legacy maintenance — read and debug old jQuery 1.x/2.x theme code.
jQuery 3 migration — find .andSelf() calls before upgrading.
Stack Overflow archaeology — translate decade-old answers to modern syntax.
Plugin upgrades — rename to .addBack() in custom extensions.
WordPress themes — many bundled scripts still reference .andSelf().
Training teams — explain why .addBack() exists and what it replaced.
🧠 How .andSelf() Used the Stack
1
Initial match
jQuery pushes the first selection onto the internal stack.
Push
2
Traverse
.find(), .nextAll(), etc. narrow the set.
Narrow
3
andSelf()
Union previous stack entry with current — same as .addBack().
Merge
4
↺
Migrate to addBack
Replace in jQuery 3 projects; behavior stays the same.
Important
📝 Notes
Available jQuery 1.2 through 2.x; deprecated in 1.8; removed in 3.0.
Identical to .addBack() with no selector argument.
Does not accept parameters — use .addBack(selector) for filtered merges.
Calling .andSelf() on jQuery 3.x throws because the method is undefined.
jQuery Migrate plugin may restore .andSelf() temporarily during upgrades — still replace in source.
For new development, read the .addBack() tutorial instead.
Compatibility
Browser Support
.andSelf() existed in jQuery 1.2 through 2.x. It was removed in jQuery 3.0. Use .addBack() in jQuery 1.8+ and all 3.x versions.
✓ Removed · jQuery 3.0
jQuery .andSelf()
Works in jQuery 1.2–2.2.4. Deprecated in 1.8. Not present in jQuery 3.x. Replacement .addBack() works in jQuery 1.8+ across all modern browsers.
LegacyjQuery 1.2–2.x only
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.addBack() replacementjQuery 1.8+
Bottom line: Learn .andSelf() to read legacy code, but write .addBack() in all new and migrated projects. Plan a find-and-replace before moving to jQuery 3.
Wrap Up
Conclusion
The jQuery .andSelf() method was the original way to merge the previous traversal stack with the current selection. It performed the same job as today’s .addBack() but was deprecated in jQuery 1.8 and removed in jQuery 3.0.
When you encounter .andSelf() in legacy code, replace it with .addBack(). For full modern syntax — including the optional selector filter — see the dedicated .addBack() tutorial.
Replace .andSelf() with .addBack() in jQuery 3 migrations
Search legacy codebases before upgrading jQuery major versions
Use .addBack(selector) when you need post-merge filtering
Read this page to understand old Stack Overflow and plugin snippets
Test chains after rename — behavior should be identical
❌ Don’t
Write .andSelf() in new jQuery 3.x projects
Assume jQuery Migrate fixes are permanent — update source code
Confuse .andSelf() with .add() or .end()
Expect a selector argument — use .addBack() instead
Load jQuery 3 and leave .andSelf() calls unchanged
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .andSelf()
Legacy stack merge — migrate to addBack.
5
Core concepts
↺01
.andSelf()
Legacy merge
API
=02
addBack
Same behavior
Replace
1.803
Deprecated
Renamed
History
3.004
Removed
Not in jQ 3
Status
📚05
Legacy
Read old code
Use case
❓ Frequently Asked Questions
.andSelf() adds the previous set of elements on jQuery's internal traversal stack to the current set — the same union behavior as .addBack() with no selector argument. It returns a new jQuery object containing both groups merged in document order.
Yes. .andSelf() and .addBack() perform the same stack merge when no selector is passed. jQuery renamed .andSelf() to .addBack() in version 1.8 for clarity. .addBack() also accepts an optional selector filter — .andSelf() does not.
Deprecated in jQuery 1.8 because the name was confusing — developers thought it referred to the current element's 'self' rather than adding the previous stack set back. Removed entirely in jQuery 3.0. Use .addBack() in all jQuery 1.8+ and 3.x code.
No. .andSelf() was removed in jQuery 3.0. Calling it on jQuery 3.7 throws a TypeError because the method no longer exists. Replace every .andSelf() with .addBack() when upgrading to jQuery 3.
Find-and-replace .andSelf() with .addBack() — a one-to-one swap for the no-argument form. If you need to filter after merging, use .addBack(selector) which .andSelf() never supported. Test chains that use .find(), .nextAll(), or .filter() before the call.
Legacy WordPress themes, jQuery 1.x plugins, Stack Overflow answers from the 2010s, and older corporate intranet codebases still contain .andSelf(). Understanding it helps you maintain and migrate that code to .addBack() and jQuery 3.
Did you know?
jQuery kept .andSelf() as an alias after introducing .addBack() in 1.8, so both names worked in jQuery 1.8 and 2.x. That overlap let developers migrate gradually. When jQuery 3.0 dropped deprecated APIs, only .addBack() survived — which is why upgrading old themes suddenly breaks on .andSelf is not a function errors.