Convert the <li> elements to an array, then alert the innerHTML of the array elements:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.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++) { document.getElementById("demo").innerHTML = x[i].innerHTML +"<br/>"; }/* w ww .jav a 2s. co m*/ }); }); </script> </head> <body> <p id="demo"></p> <button>Alert the value of each list item</button> <ul> <li>CSS</li> <li>HTML</li> <li>Javascript</li> </ul> </body> </html>
The toArray()
method returns the elements matched by the jQuery selector as an array.
$(selector).toArray()