In this case, it was not necessary to declare i
. The signature of each
method is specified in jQuery docs:
.each( function(index, Element) )
As you can see, it takes one argument, and this argument is a function that takes 2 arguments, index
and Element
.
In your code, i
is the identifier for index
, which is passed to the jQuery callback function each time it is called (once for each iteration). It also passes the second argument, but you did not give it an identifier, so it will only be available through the arguments
object.
You can see exactly what is being passed to the callback by registering this arguments
object:
β$("div").each(function () {
Here is a working example above.
Here's the corresponding code from jQuery itself (comments added):
//... each: function( object, callback, args ) { //... //Iterate over the matched set of elements for ( ; i < length; ) { /* Call the callback in the context of the element, passing in the index and the element */ if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) { break; } } }
source share