jQuery .pushStack() Method

Intermediate
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Selection stack

What You’ll Learn

.pushStack(elements) builds a new jQuery object from an array of DOM nodes and pushes it onto the internal selection stack (same API as api.jquery.com/pushStack). This tutorial covers the official demo, pairing with .end(), plugin patterns, the optional name/arguments form, five examples, and try-it labs.

01

Push set

From array

02

Stack

Keep previous

03

.end()

Pop back

04

Plugins

Custom traversal

05

name / args

Since 1.3

06

Since 1.0

Core API

Introduction

When you call .find() or .filter(), jQuery does not throw away the previous match — it keeps a stack of selections so .end() can walk back. That push behavior is exposed as .pushStack().

Since jQuery 1.0, .pushStack(elements) lets you take any array-like list of DOM nodes, wrap them as the current jQuery set, and still recover the earlier set with .end(). Plugin authors use it so custom methods feel like built-in traversal.

You rarely need .pushStack() in app UI code — prefer .find(), .filter(), and .end(). Learn it to understand the stack and to write chain-friendly plugins. See the Traversing hub.

Understanding the .pushStack() Method

Call .pushStack(elements) on a jQuery object and pass an array (or array-like collection) of DOM elements. jQuery creates a new collection from those nodes and remembers the previous collection on an internal stack.

The official demo starts from an empty set, pushes every <div> on the page, removes them, then .end() returns to the empty starter set.

💡
Beginner Tip

Think of .pushStack() as “make this array the current selection, but remember where I was.” .end() is “go back one memory.”

📝 Syntax

Signatures:

jQuery
.pushStack( elements )                      // 1.0+
.pushStack( elements, name, arguments )     // 1.3+

Parameters

  • elements (Array) — DOM elements to wrap as the new jQuery object.
  • name (String, optional) — name of the method that produced the array (serialization / debugging).
  • arguments (Array, optional) — arguments passed to that method.

Return value

  • A new jQuery object containing elements, stacked so .end() can restore the previous set.

Official demo

jQuery
jQuery( [] )
  .pushStack( document.getElementsByTagName( "div" ) )
  .remove()
  .end();

⚡ Quick Reference

GoalCode
Push a NodeList / array$(x).pushStack( Array.from(nodes) )
Return to previous set.pushStack( els ).doStuff().end()
Plugin custom setreturn this.pushStack( matched )
With method metadata.pushStack( els, "myFind", [sel] )
Input
Array

DOM elements to become the set

Keeps
prev

Previous selection on the stack

Partner
.end()

Pops back one level

Audience
plugins

Custom traversal helpers

📋 .pushStack() vs .end() vs .add()

.pushStack(els).end().add()
RolePush a new setPop previous setMerge more elements
StackGrows by oneShrinks by oneUses stack rules of .add
Best forCustom / plugin setsUndo last filterUnion with selectors

Examples Gallery

Examples follow the official API and common plugin patterns. Use View Output or Try It Yourself to explore each case.

📚 Getting Started

Push a set, act on it, then understand the stack.

Example 1 — Official Demo: Push Divs, Remove, End

Start empty, push every div, remove them, then .end().

jQuery
jQuery( [] )
  .pushStack( document.getElementsByTagName( "div" ) )
  .remove()
  .end();
Try It Yourself

How It Works

getElementsByTagName returns a live HTMLCollection; jQuery accepts it as the elements array. .remove() acts on the pushed set; .end() restores jQuery([]).

Example 2 — Style Pushed Nodes, Then End

Push specific elements, change CSS, return to the previous selection.

jQuery
$( "#host" )
  .css( "outline", "2px solid #94a3b8" )
  .pushStack( $( ".item" ).get() )
  .css( "background", "#fef08a" )
  .end()
  .append( "

Back on #host

" );
Try It Yourself

How It Works

.get() returns a plain array of DOM nodes — perfect input for .pushStack(). After .end(), chaining continues on #host.

📈 Practical Patterns

Plugins, comparison with built-ins, and the metadata overload.

Example 3 — Plugin That Supports .end()

Return this.pushStack(matched) from a custom traversal helper.

jQuery
$.fn.oddChildren = function() {
  var matched = this.children().filter( ":odd" ).get();
  return this.pushStack( matched );
};

$( "#list" )
  .oddChildren()
  .css( "color", "crimson" )
  .end()
  .css( "border", "1px solid #cbd5e1" );
Try It Yourself

How It Works

If the plugin returned a bare $(matched) without pushStack, .end() would not restore #list. Pushing keeps chaining predictable.

Example 4 — .pushStack() vs Built-in .find()

Built-ins already push; you only need pushStack for custom arrays.

jQuery
// Everyday — find already stacks for .end()
$( "#box" ).find( "span" ).addClass( "x" ).end();

// Custom array — you must pushStack yourself
var nodes = document.querySelectorAll( "#box > span" );
$( "#box" ).pushStack( Array.from( nodes ) ).addClass( "y" ).end();
Try It Yourself

How It Works

Reach for .pushStack() when the next set comes from native APIs, algorithms, or filtered arrays that jQuery did not produce for you.

Example 5 — Name and Arguments (1.3+)

Optional metadata describing how the set was built.

jQuery
$.fn.takeFirst = function( n ) {
  var els = this.get().slice( 0, n );
  return this.pushStack( els, "takeFirst", [ n ] );
};

$( "li" ).takeFirst( 2 ).addClass( "picked" ).end();
Try It Yourself

How It Works

The name and arguments parameters help jQuery record how the stack entry was created. Behavior for chaining is the same as the simple overload.

🚀 Common Use Cases

  • Plugin traversal — return this.pushStack(matched) so callers can .end().
  • Native DOM lists — wrap querySelectorAll / getElementsByTagName results in a stackable set.
  • Algorithmic filters — push a subset computed in plain JavaScript.
  • Understanding .find / .filter — see what built-ins do under the hood.
  • Temporary work sets — act on a pushed group, then restore with .end().
  • Not for simple UI — use .find() / .filter() when selectors suffice.

🧠 How .pushStack() Fits

1

Current selection

You have a jQuery object (maybe empty, maybe #host).

Base
2

Push new elements

.pushStack(array) makes that array the active set.

Push
3

Work on the new set

Call .css(), .remove(), plugins, and so on.

Act
4

.end() restores

The previous selection becomes current again.

📝 Notes

  • Added in jQuery 1.0; name/arguments overload since 1.3.
  • Pass an array (or array-like) of DOM elements — not a selector string.
  • Built-in methods like .find() already call the stack machinery for you.
  • Pair with .end(); related: .addBack().
  • Returning $(els) from a plugin without pushStack breaks .end() expectations.
  • Primarily an internals / plugin API — intermediate topic for learners.

Browser Support

.pushStack() has been part of jQuery since 1.0+. It works in every browser supported by your jQuery build — including modern evergreen browsers with jQuery 3.x. Stack behavior is implemented by jQuery.

jQuery 1.0+

.pushStack()

Stable selection-stack helper used by plugins and built-in traversal methods.

100% With jQuery 1.0+
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
.pushStack() 1.0+

Bottom line: Use in plugins that return a new set; prefer .find()/.filter() in app code.

Conclusion

.pushStack() pushes a new array of DOM elements onto jQuery’s selection stack so .end() can restore the previous set. It is the building block behind friendly chaining in plugins and custom traversal helpers.

Next up: .eq() to reduce a set to a single element by index.

💡 Best Practices

✅ Do

  • Return this.pushStack(matched) from custom traversal plugins
  • Pass a real array of DOM nodes (.get() / Array.from)
  • Document that callers can use .end() after your method
  • Prefer .find() / .filter() when selectors are enough
  • Test chains that mix your method with .end()

❌ Don’t

  • Pass a selector string where an elements array is required
  • Return a disconnected $(els) if you want .end() support
  • Use pushStack for simple app chaining when built-ins suffice
  • Forget that pushed elements are the current set until .end()
  • Confuse pushStack with .add() (merge vs replace+stack)

Key Takeaways

Knowledge Unlocked

Five things to remember about .pushStack()

The selection-stack push used by plugins and internals.

5
Core concepts
02

Keeps

Previous

Stack
03

Pair

.end()

Pop
04

For

Plugins

Custom
1.0 05

Since

1.0

API

❓ Frequently Asked Questions

.pushStack(elements) takes an array of DOM elements, wraps them in a new jQuery object, and pushes that object onto jQuery’s internal selection stack. The previous set is preserved so a later .end() can restore it. Available since jQuery 1.0.
Mostly plugin and library authors who build custom traversal methods. Everyday code usually uses .find(), .filter(), .add(), and .end() — those already manage the stack for you. Learn .pushStack() so custom methods can support .end() correctly.
.pushStack() pushes a new set. .end() pops one level and returns the previous set. The official demo pushes all divs, calls .remove(), then .end() to leave the earlier (empty) set. Without pushStack, a custom set would not be reversible with .end().
Since jQuery 1.3 you can pass .pushStack(elements, name, args) where name is the method that produced the elements and args is the argument list used for serialization / debugging. Day-to-day plugins often use the simple .pushStack(elements) form.
.add() merges extra elements into the current selection (or starts from them). .pushStack() replaces the current matched set with a new jQuery object built from an array, while stacking the old set for .end(). They solve different problems.
A new jQuery object containing the elements you passed. You can chain methods on it like any other collection.
Did you know?

Many of jQuery’s own traversal methods are thin wrappers around collecting nodes and calling the same stack machinery that .pushStack() exposes. When $("ul").find("li").end() works, you are using the same idea the official empty-set + getElementsByTagName demo shows in the open.

Next: .eq() Method

Reduce the matched set to the single element at a given index.

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

8 people found this page helpful