Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is toArray() method in jquery ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 494
    Comment on it

    Hello Readers,

    toArray() is the jquery method which is used to convert the elements to array. This method returns the jquery elements matched by the jquery selector as an array.

    Syntax :

    $(selector).toArray()
    

    Code Example :

    <html> 
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            var i;
            var x = $("li").toArray()
            for (i = 0; i< x.length; i++) {
                alert(x[i].innerHTML);
            }
        });
    });
    </script>
    </head>
    <body>
    
    <button>button</button>
    
    <ul>
      <li>Coffee</li>
      <li>Milk</li>
      <li>Soda</li>
    </ul>
    
    </body>
    </html>  
    

    In the above code use the toArray() method and convert the li elements to an array and alert the message of the given elements as an array in jquery.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: