Javascript examples for jQuery Method and Property:each
The each() method run a function for each matched element.
Parameter | Require | Description |
---|---|---|
function(index,element) | Required. | A function to run for each matched element. |
The following code shows how to Alert the text of each <li> element:
<!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(){ $("li").each(function(){ console.log($(this).text()) });// w ww . ja va 2 s.co m }); }); </script> </head> <body> <button>Alert the value of each list item</button> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html>