JQuery | makeArray() Method

This makeArray() method in jQuery is used to convert an array-like object into a true JavaScript array.

Syntax:

jQuery.makeArray( obj )

Parameters: This method accept a single parameter which is mentioned above and described below:

  • obj : This parameter holds an object to turn into a native Array.

Return Value: It returns the array.

Below examples illustrate the use of makeArray() method in jQuery:



Example 1: This example use jQuery.makeArray() method and turn a collection of HTMLElements into an Array of them.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | makeArray() method</title>
    <script src=
    </script>
  
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green"
        GeeksforGeeks 
    </h1>
  
    <h3>JQuery | makeArray () method</h3>
    <p>Geeks1</p>
    <p>Geeks2</p>
    <p>Geeks3</p>
    <button onclick="geek()">Click</button>
    <br>
    <br>
    <b id="root"></b>
  
    <script>
        function geek() {
            var elems = document.getElementsByTagName("p");
            var x = jQuery.makeArray(elems);
            var t = x[1];
            x[1] = x[2];
            x[2] = t;
            $(x).appendTo(document.body);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2: This example use jQuery.makeArray() method and convert an array-like string into the actual array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <title>JQuery | makeArray() method</title>
    <script src=
    </script>
  
</head>
  
<body style="text-align:center;">
  
    <h1 style="color: green"
        GeeksforGeeks 
    </h1>
  
    <h3>JQuery | makeArray() method</h3>
    <b>String = "[2, 5, 6, 3, 8, 9]"</b>
    <br>
    <br>
    <button onclick="geek()">Click</button>
    <br>
    <br>
    <b id="root"></b>
  
    <script>
        function geek() {
            var el = document.getElementById('root');
            var arr = "[2, 5, 6, 3, 8, 9]";
  
            var newArr = jQuery.makeArray(arr)
            el.innerHTML = jQuery.type(newArr) + " = " + newArr;
        }
    </script>
</body>
  
</html>                   

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.