.pushStack() JQ Home  <<  Core & Internals  <<  .pushStack()

Stack Elements.

Shorthand version $.pushStack()

Description

The .pushStack() method allows us to put a collection of DOM elements onto the jQuery stack.

Syntax

Signature Description
.pushStack( elementArray )Put a collection of DOM elements onto the jQuery stack.
.pushStack( elementArray, methodName, methodArgs )Put a collection of DOM elements onto the jQuery stack passing across the elementArray along with the methodName and methodArgs used to create it.

Parameters

Parameter Description Type
elementArrayAn array of elements to push onto the stack and make into a new jQuery object.Array
methodNamejQuery method name that created the elementArray.String
methodArgsArguments passed to the jQuery method.Array

Return

A jQuery object either containing the elements matched or empty.

.pushStack( elementArray ) Example Core  <<  Top

Put a collection of DOM elements onto the jQuery stack.

In the following example we just create a new jQuery object by pushing all 'p' nodes from this page onto the stack and alert it.


$(function(){
  $('#btn4').on('click', function() { 
    var $hold = $().pushStack( document.getElementsByTagName('p') );
    alert('Our object: ' + JSON.stringify($hold));
  });
}); 

Press the button below to action the above code:

.pushStack( elementArray, methodName,
                      methodArgs )
Example Core  <<  Top

Put a collection of DOM elements onto the jQuery stack passing across the elementArray along with the methodName and methodArgs used to create it.

In the following example we just create a variable called '$pArray' and then push it onto the stack and alert it


$(function(){
  $('#btn5').on('click', function() { 
    var $pArray = $('p').children('em');
    var $hold = $().pushStack( $pArray, 'children', 'em' );
    alert('Our object: ' + JSON.stringify($hold));
  });
}); 

Press the button below to action the above code:

Related Tutorials

jQuery Basic Tutorials - Lesson 2 - jQuery Core & Internals