toArray() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:toArray

Description

The toArray() method returns the elements matched by the jQuery selector as an array.

The following code shows how to Convert the <li> elements to an array, then alert the innerHTML of the array elements:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var i;
        var x = $("li").toArray()
        for (i = 0; i< x.length; i++) {
            console.log(x[i].innerHTML);
        }//from   www . j  a v  a2  s .  c om
    });
});
</script>
</head>
<body>

<button>Alert the value of each list item</button>

<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
</ul>

</body>
</html>

Related Tutorials