Wednesday, September 28, 2011

each(Function)

each(Function fn) returns jQuery
Execute a function within the context of every matched element. This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points
to the specific DOM element.

Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index).
Example:
Iterates over two images and sets their src property
$("img").each(function(i){
this.src = "test" + i + ".jpg";
});

HTML:
<img/><img/>

Result:
<img src="test0.jpg"/><img src="test1.jpg"/>

No comments:

Post a Comment