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)