each() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:each

Description

The each() method run a function for each matched element.

Syntax

Parameter Require Description
function(index,element) Required. A function to run for each matched element.
  • index - index position of the selector
  • element - current element (the "this" selector can also be used)

The following code shows how to Alert the text of each <li> element:

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(){
        $("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>

Related Tutorials