.each()
methodThe .each(function)
method in jQuery is used to iterate through the matched elements set and execute a function
for every element.
return false
in the function
to break out of each()
loops.this
refers to the DOM element.$(this)
.each()
method Syntax.each(function)
.each()
method ExampleIterate over unordered list and add a new class highlight
HTML
<ul> <li>java tutorial</li> <li>learn javascript</li> <li>learn jquery</li> </ul>
jQuery
$('li').each(function(){ $(this).addClass('highlight') });
Result
<ul> <li class="highlight">java tutorial</li> <li class="highlight">learn javascript</li> <li class="highlight">learn jquery</li> </ul>