Tuesday, May 1, 2012

jQuery .each() this and element


Inside a .each() callback, is there any difference between this and the second argument of the callback function?



For example, in the following code:




$("example").each( function(index, element) {
// body
});



is there any difference between this and element ? Is the second argument just provided so you can choose a name?


Source: Tips4all

1 comment:

  1. Nope, there's no difference; the second argument is just for convenience.


    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.

    from http://api.jquery.com/each/


    Most likely, the second argument is provided for consistency with jQuery.each.

    ReplyDelete