jQuery Tutorial Tutorials - jQuery .each() method

jQuery .each() method

The .each(function) method in jQuery is used to iterate through the matched elements set and execute a function for every element.

  • You can use return false in the function to break out of each() loops.
  • this refers to the DOM element.
  • If you want to access jQuery object, use $(this)

jQuery .each() method Syntax

.each(function)

jQuery .each() method Example

example

Iterate 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>

Try now

Date:2019-08-26 13:25:24 From:www.Lautturi.com author:Lautturi