Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>.pushStack()</code> creates a new jQuery object that inherits state from a previous jQuery object.</p> <p>This inherited state allows methods like <code>.end()</code> and <code>.self()</code> to work properly. In your particular code example, you aren't using the return value from <code>.pushStack()</code> which is the new jQuery object.</p> <p>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.</p> <p>In your case, I don't think you need to use <code>.pushStack()</code> at all and you can probably just use <code>.add()</code> (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.</p> <p>To achieve the end result HTML you showed in your question, you could do this:</p> <pre><code>$(function() { $("p").add("div").css("color", "#f00").appendTo(document.body); });​ </code></pre> <p>You could obviously change the <code>.appendTo()</code> to whatever you intend to do with the new DOM objects.</p> <p>In your comments you asked some more about when one would use <code>pushStack()</code>. 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 <code>return this.pushStack(elems)</code>. 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 <code>.end()</code> can work when used in chaining.</p> <p>The jQuery <code>.add()</code> method is a classic example. In pseudo-code, what it does is this:</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload