Home >>jQuery Tutorial >jQuery each() Method

jQuery each() Method

jQuery each() Method

jQuery each() method in jQuery is used to specifies a function to run for each matched element.

Syntax:
$(selector).each(function(index,element))

Parameter Values

Parameter Description
function(index,element) It is a required parameter which is used to run a function for each matched element.
  • index – Used to the index position of the selector
  • element – Used to the current
Here is an Example of jQuery each() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$("li").each(function(){
alert($(this).text())
});
});
});
</script>
</head>
<body>
<button class="btn1">Click me to alert the value of each list item</button>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>

Output:
  • One
  • Two
  • Three

No Sidebar ads