Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:56:21+00:00 2026-06-02T07:56:21+00:00

I’m having trouble understanding the jQuery pushStack function (documented at http://api.jquery.com/pushStack/ ). I have

  • 0

I’m having trouble understanding the jQuery pushStack function (documented at http://api.jquery.com/pushStack/). I have tried to add a list of items to a selector in this Fiddle.

http://jsfiddle.net/johnhoffman/hTh8D/

JS:

$(function() {
   var listStuff = $("p");
   listStuff.pushStack($("div"));
   listStuff.css("color", "#f00");        
});​

HTML:

<p>foo</p>
<div>bar</div>​

However, the text of the div element is not red. What does it mean to push an element onto a jQuery stack?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:56:22+00:00Added an answer on June 2, 2026 at 7:56 am

    .pushStack() creates a new jQuery object that inherits state from a previous jQuery object.

    This inherited state allows methods like .end() and .self() to work properly. In your particular code example, you aren’t using the return value from .pushStack() which is the new jQuery object.

    When working with jQuery objects, it’s important to know that most operations that change what DOM objects are in a jQuery object return a new jQuery object with the change in it rather than modifying the existing jQuery object. It is this design characteristic that allows them to maintain this stack of previously modified jQuery objects.

    In your case, I don’t think you need to use .pushStack() at all and you can probably just use .add() (which also returns a new jQuery object) but it isn’t exactly clear to me what you’re trying to do so I’m not sure exactly what code to recommend.

    To achieve the end result HTML you showed in your question, you could do this:

    $(function() {
       $("p").add("div").css("color", "#f00").appendTo(document.body);        
    });​
    

    You could obviously change the .appendTo() to whatever you intend to do with the new DOM objects.

    In your comments you asked some more about when one would use pushStack(). I think the main use is in jQuery methods that return a new jQuery object. In that case, you create the new list of elements you want in the new jQuery object and then rather than just turning that into a regular jQuery object and returning it, you call return this.pushStack(elems). This ends up creating a new jQuery object and returning it, but it also links that new object to the previous object so that special commands like .end() can work when used in chaining.

    The jQuery .add() method is a classic example. In pseudo-code, what it does is this:

    add: function(selector, context) {
        // get the DOM elements that correspond to the new selector (the ones to be added)
        // get the DOM elements from the current jQuery object with this.get()
        // merge the two arrays of elements together into one array
        return this.pushStack(combinedElems);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.