1.0 A function to execute for each matched element. Iterate over a jQuery object, executing a function for each matched element.

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

Suppose we had a simple unordered list on the page:

<ul>
    <li>foo</li>
    <li>bar</li>
</ul>
    

We can select the list items and iterate across them:

$('li').each(function(index) {
    alert(index + ': ' + $(this).text());
});
  

A message is thus alerted for each item in the list:

0: foo
1: bar

We can stop the loop from within the callback function by returning false.

Iterates over three divs and sets their color property. Click here
to iterate through
these divs.
]]>
If you want to have the jQuery object instead of the regular DOM element, use the $(this) function, for example: (click here to change) ]]> You can use 'return' to break out of each() loops early. Change colors
Stop here
]]>