Iterate over a JQuery object, executing a function for each matched element.
. each (function)
function
Type:function (Integer index, Element Element)
A function to execute for each matched element
When called it iterates over the DOM elements that is part of the JQuery object. Each time the callback runs, it's passed the current loop iteration, andbegginning from 0.
The callback is fired in the context of the "the current DOM element," so the "keyword this refers to the" C3>element. This = = Element
Example
<ul>
<li>foo</li>
<li>bar</li>
</ul>
$ (' Li '). each (function (index) {
Console.log (Index + ":" + $ (This). text ());
})
Output:
0:foo
1:bar
You can stop the loop from within the callback function by returning false.
$( "div" ).each(function( index, element ) {
// element == this
$( element ).css( "backgroundColor", "yellow" );
if ( $( this ).is( "#stop" ) ) {
$( "span" ).text( "Stopped at div index #" + index );
return false;
}
});
JQuery:. each (function)