Open In App

jQuery pushStack() Method

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The pushStack() method in jQuery is used to add a collection of DOM elements onto the jQuery stack.

Syntax:

.pushStack(elements, name, arguments)

Parameters:

  • elements: This is the array of elements to be pushed onto the stack and make into a new jQuery object.
  • name: This argument defines the name of a jQuery method which has generated the array of elements.
  • arguments: The argument that were passed in to the jQuery method for serialization.

Example 1:

html
<!DOCTYPE HTML>
<html>

<head>
    <title>
        JQuery pushStack() method
    </title>

    <script src=
"https://code.jquery.com/jquery-3.5.0.js">
    </script>
</head>

<body style="text-align:center;">

    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    
    <p>
        JQuery | pushStack() method
    </p>

    <div> 
        This is div
    </div>
    <br>

    <button onclick="Geeks()">
        Click here
    </button>
    
    <p id="GFG"></p>

    <script>
        var el_down = document.getElementById("GFG");

        function Geeks() {
            jQuery([]).pushStack(
                document.getElementsByTagName("div"))
                .remove().end();

            el_down.innerHTML
                = "The DOM element &lt;div&gt; has "
                    + "been pushed to stack and then"
                    + " removed.";
        } 
    </script>
</body>

</html> 
  • Output:

  • Example 2: This example checked the odd indexed checkboxes.

    HTML
    <!DOCTYPE HTML>
    <html>
    
    <head>
        <title>
            JQuery pushStack() method
        </title>
    
        <script src=
    "https://code.jquery.com/jquery-3.5.0.js">
        </script>
    </head>
    
    <body style="text-align:center;">
    
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
        
        <p>JQuery | pushStack() method</p>
    
        <div>This is div</div>
        <br>
    
        <button onclick="Geeks()">
            Click here
        </button>
        
        <div id="GFG"></div>
    
        <script>
    
            var el_down = document.getElementById("GFG");
    
            function Geeks() {
                jQuery([]).pushStack(
                    document.getElementsByTagName("p"))
                    .remove().end();
    
                el_down.innerHTML = "The DOM element "
                    + "&lt;p&gt; containing 'JQuery |"
                    + " pushStack() method has been "
                    + "pushed to stack and then removed.";
            } 
        </script>
    </body>
    
    </html>
    

    Output:



    Next Article

    Similar Reads