jQuery .pushStack() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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]);
CallWhat it does
elementsArray (or array-like) of DOM nodes
nameMethod name for serialization
argumentsArgs list for that method
Return valueNew jQuery object (stacked)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Collect DOM elements into an array (or array-like list).
Step 2Call .pushStack(elements) on a jQuery object.
Step 3Chain methods on the new set.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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

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: .uniqueSort() Method

Reduce a matched set to a single element by index.

.uniqueSort() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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