jQuery | Misc toArray() Method

The toArray() Method in jQuery is used to return the elements that are matched by the jQuery selector as an array.

Syntax

$(selector).toArray()

Parameters: This method does not accept any parameters.



Example 1: This method uses toArray() method to display the paragraph in form of array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title>
        jQuery Misc toArray() Method
    </title>
      
    <script src=
    </script>
</head
  
<body style="text-align:center;">
      
    <h1 style = "color:green;" >  
        GeeksForGeeks
    </h1>  
      
    <h2>jQuery Misc toArray() Method</h2>
      
    <button>Click</button>
      
    <p>Geeks1</p>
    <p>Geeks2</p>
    <p>Geeks3</p>
      
    <!-- Script to use toArray() method -->
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                var i;
                var x = $("p").toArray()
                for (i = 0; i< x.length; i++) {
                    alert(x[i].innerHTML);
                }
            });
        });
    </script>
</body>
  
</html>  

chevron_right


Output:

  • Before Click on the button:
  • After Click on the button:


Example 2: This example use toArray() method to display the array element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title>
        jQuery Misc toArray() Method
    </title>
      
    <script src=
    </script>
</head
  
<body style="text-align:center">
  
    <h1 style="color:green;">  
        GeeksForGeeks
    </h1>  
      
    <h2>jQuery Misc toArray() Method</h2>
      
    <button>Click</button>
      
    <p>Geeks1-Geeks2-Geeks3</p>
      
    <div style="color:lightgreen;"></div>
      
    <!-- This example use toArray() method -->
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                var i;
                var x = $("p").toArray()
                for (i = 0; i< x.length; i++) {
                    $("div").text(x[i].innerHTML);
                }
            });
        });
    </script>
</body>
  
</html>  

chevron_right


Output:

  • Before Click on the button:
  • After Click on the button:


My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.