jQuery .andSelf() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Legacy API

What You’ll Learn

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

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.

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.

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

Official jQuery API description

jQuery
// Legacy (jQuery 1.2–2.x):
$( "li.third-item" ).nextAll().andSelf()
  .css( "background-color", "red" );

// Modern replacement (jQuery 1.8+):
$( "li.third-item" ).nextAll().addBack()
  .css( "background-color", "red" );

⚡ Quick Reference

GoalCode
Legacy stack merge.nextAll().andSelf()
Modern replacement.nextAll().addBack()
Container + children (legacy).find("p").andSelf()
Filter after merge (modern only).addBack(".active")
Revert without merge.end()
Status in jQuery 3+Removed — use .addBack()

📋 .andSelf() vs .addBack() vs .end()

Legacy name, modern replacement, and the non-merge alternative.

.andSelf()
legacy

jQuery 1.2–2.x; removed in 3.0

.addBack()
modern

jQuery 1.8+; optional selector

.end()
prev only

No merge — previous set alone

jQuery 3.x
addBack

andSelf throws — method missing

Examples Gallery

Examples show legacy .andSelf() syntax alongside modern .addBack() equivalents. Try-it labs use jQuery 2.2.4 where .andSelf() still exists.

📚 Getting Started

Classic stack-merge chains from legacy jQuery codebases.

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

Highlight list items 3, 4, and 5 — the same pattern documented for .addBack(), written with the old method name.

jQuery
// jQuery 1.2–2.x
$( "li.third-item" ).nextAll().andSelf()
  .css( "background-color", "red" );

// jQuery 1.8+ / 3.x — use instead:
$( "li.third-item" ).nextAll().addBack()
  .css( "background-color", "red" );
Try It Yourself

How It Works

nextAll() selects items 4 and 5; the stack remembers item 3. .andSelf() unions both groups — identical to .addBack() in this form.

📈 Practical Patterns

Container styling, migration, stack comparison, and legacy code reading.

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

After .find("p"), only paragraphs match. .andSelf() adds the parent div back into the collection.

jQuery
// Paragraphs only:
$( "div.panel" ).find( "p" ).addClass( "highlight" );

// Div AND paragraphs (legacy):
$( "div.panel" ).find( "p" ).andSelf().addClass( "highlight" );

// Modern equivalent:
$( "div.panel" ).find( "p" ).addBack().addClass( "highlight" );
Try It Yourself

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" );
Try It Yourself

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;
Try It Yourself

How It Works

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

jQuery
// Typical 2012-era plugin pattern:
$.fn.highlightSection = function () {
  return this.find( ".content" )
    .andSelf()  // ← merge wrapper + .content nodes
    .addClass( "section-active" );
};

// Modern rewrite for jQuery 3:
$.fn.highlightSection = function () {
  return this.find( ".content" )
    .addBack()
    .addClass( "section-active" );
};
Try It Yourself

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.

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

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

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.

Legacy jQuery 1.2–2.x only
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
.addBack() replacement jQuery 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about .andSelf()

Legacy stack merge — migrate to addBack.

5
Core concepts
= 02

addBack

Same behavior

Replace
1.8 03

Deprecated

Renamed

History
3.0 04

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.

Continue to jQuery .children()

Select direct child elements one level down the DOM tree.

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

6 people found this page helpful