Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .each() this and element

Tags:

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?

like image 594
rubergly Avatar asked Jun 16 '11 20:06

rubergly


1 Answers

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.

like image 122
Sophie Alpert Avatar answered Oct 18 '22 10:10

Sophie Alpert