jQuery .pushStack() Method

Intermediate
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .pushStack()?

.pushStack(elements) (since jQuery 1.0) builds a new jQuery object from an array of DOM elements and pushes it onto the selection stack. The previous set stays available for .end().

Remember
// Official pattern
jQuery([])
  .pushStack(document.getElementsByTagName("div"))
  .remove()
  .end();

// Plugin idea — custom set + reversible with .end()
return this.pushStack(matchedElements);

// Since 1.3 (optional metadata)
.pushStack(elements, "myMethod", [arg1, arg2]);
NeedUse
Custom set that supports .end().pushStack(array)
Pop back one level.end()
Merge into current set.add()
Restore previous + current.addBack()
Normal traversal.find() / .filter()

How to Use .pushStack()

Start from a jQuery object, push a new element array, then chain or .end().

  1. Collect DOM elements into an array (or array-like list).
  2. Call .pushStack(elements) on a jQuery object.
  3. Chain methods on the new set.
  4. Call .end() when you want the previous set back.
  5. In plugins, return this.pushStack(…) so callers can use .end().
PieceRequired?Role
elementsYesArray (or array-like) of DOM nodes
nameNo (1.3+)Method name for serialization
argumentsNo (1.3+)Args list for that method
Return value—New jQuery object (stacked)

Pseudocode

Pseudocode
call $(currentSet).pushStack(elements)

remember currentSet as "previous"
newSet = jQuery object wrapping elements
link newSet.prevObject = previous

return newSet

later:
  .end() → return previous set

Cheat Sheet

Keep these facts handy when writing custom chains.

TopicDetail
Syntax.pushStack(elements[, name, args])
Since1.0 (name/args since 1.3)
InputArray of DOM elements
StackPushes new set; old set kept for .end()
AudiencePlugins / custom traversal
vs .add()Replace+stack vs merge into set
Official demojQuery([]).pushStack(divs).remove().end()

Live Preview

Push boxes onto the stack, mark them, then .end() and compare lengths.

Starter set is empty ($([])). .pushStack() loads the boxes; .end() returns to length 0.

A
B
C

Paragraph stays

Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Official pushStack + remove + end

Push all .box divs, remove them, then pop the stack (CSS-free).

jQuery
jQuery([])
  .pushStack(document.getElementsByTagName("div"))
  .filter(".box")
  .remove()
  .end();
$("#out").text("Boxes removed. Paragraph remains.");
Try It Yourself

How It Works

  1. Start. An empty jQuery set is the stack base.
  2. Push. All div nodes become the active set.
  3. Filter. Keep only .box, then remove them.
  4. End. Return to the empty starter set.

Example 2 — .end() Restores the Previous Length

Show that .pushStack() is reversible with .end().

jQuery
var $start = $("#list li");
var $pushed = $start.pushStack(
  document.querySelectorAll("#list span")
);
$("#out").text(
  "start → " + $start.length +
  " | pushed → " + $pushed.length +
  " | end → " + $pushed.end().length
);
Try It Yourself

How It Works

  1. Start. Two list items are the previous set.
  2. Push. Two spans become the active set.
  3. End. Pops back to the list-item set.
  4. Point. Lengths prove the stack was preserved.

Example 3 — Tiny Plugin Using pushStack

Return a custom set so callers can chain and .end().

jQuery
$.fn.oddItems = function () {
  return this.pushStack(this.filter(":odd").get());
};

$("#list li").oddItems().append(" [odd]").end()
  .append(" [all]");
Try It Yourself

How It Works

  1. Filter. Odd-index items are collected with .get().
  2. Push. pushStack makes that array a chained set.
  3. Mark. Only odd items get [odd].
  4. End. Back on the full list, every item gets [all].

Example 4 — .pushStack() vs .add()

Replace+stack vs merge into the current selection.

jQuery
var $li = $("#list li");
$("#out").text(
  "add → " + $li.add("span").length +
  " | pushStack → " + $li.pushStack(
    document.querySelectorAll("#list span")
  ).length
);
Try It Yourself

How It Works

  1. add. Merges spans into the li set (combined length).
  2. pushStack. Switches to spans only (new set length).
  3. Stack. Only pushStack stores the old li set for .end().
  4. Pick. Merge with .add(); replace for plugins with .pushStack().

Example 5 — name and arguments (1.3+)

Optional metadata for serialization — same push behavior.

jQuery
var nodes = document.querySelectorAll(".box");
var $set = $([]).pushStack(nodes, "boxSet", [".box"]);
$("#out").text(
  "length → " + $set.length +
  " | after end → " + $set.end().length
);
Try It Yourself

How It Works

  1. Elements. Pass the DOM node list as usual.
  2. Name / args. Optional metadata for serialization (since 1.3).
  3. Behavior. The active set still contains the three boxes.
  4. Daily use. Most plugins can omit name/args and pass elements only.

Edge Cases & Pitfalls

Check these before using .pushStack() in app code.

audience

Not for everyday selection

Prefer .find() / .filter() / .add(). Reach for .pushStack() when you write a custom method that must support .end().

input

Pass DOM elements, not a selector string

.pushStack("div") is wrong. Pass an array or array-like list of nodes (e.g. from .get() or querySelectorAll).

wrap

$(array) is not the same

Wrapping with $() does not stack the previous set. Without .pushStack(), .end() will not return to your prior collection.

end

Remember to .end() in demos

If you push and mutate, call .end() when the chain should continue on the previous set — just like the official example.

Browser Support

.pushStack() is a core jQuery method since 1.0 (name/arguments overload since 1.3). It works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.0+

jQuery .pushStack()

Universal across jQuery-supported browsers. Mainly used inside plugins that need reversible custom sets.

1.0+ jQuery since
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() Universal

Bottom line: Safe and stable. Learn it for plugin authoring; prefer built-in traversal for normal pages.

Performance Notes

ApproachCostBest for
.find() / .filter()Cheapest habitEveryday traversal (already stacked)
.pushStack(smallArray)CheapPlugin return values
Huge node lists repeatedlyModerateAvoid building giant arrays needlessly

The stack link itself is cheap. Cost comes from collecting and operating on large element arrays — keep plugin results focused.

Key Takeaways

  • Role: push a new element array onto the selection stack.
  • Pair: use with .end() to restore the previous set.
  • Audience: plugin authors more than day-to-day page scripts.
  • vs .add(): replace+stack vs merge into the current set.

One line: .pushStack() makes a custom element array a proper jQuery chain step so .end() can undo it.

Frequently Asked Questions

.pushStack(elements) wraps an array of DOM elements in a new jQuery object and pushes it onto jQuery’s internal selection stack. The previous set is kept 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. Learn .pushStack() so custom methods can support .end() correctly.
.pushStack() pushes a new set. .end() pops one level and returns the previous set. Without .pushStack(), a hand-built collection may 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 for serialization. Day-to-day plugins often use the simple .pushStack(elements) form.
.add() merges extra elements into the current selection. .pushStack() replaces the current matched set with a new jQuery object from an array, while stacking the old set for .end().
A new jQuery object containing the elements you passed. You can chain methods on it like any other collection.

Next: .eq() method

Reduce a matched set to a single element by 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