Hello Readers,
each() is the jquery function and it is used to iterate over jquery matched object list.
each() is a traversing method which works only jquery objects.
syntax :
$(selector).each(function(index,element))
Parameters :
function(index,element) :this is a required parameter which run over each matched elements.
index - index is the (index) position of the selector starting with zero.
element -this is the current element of the selector.
Code Example of the each() function :
<!DOCTYPE html>
<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(){
$("li").each(function(){
alert($(this).text())
});
});
});
</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>
In the above code, li selector is used with .each() function and alert the current text.
0 Comment(s)