PHP Coder
PHP Coder

Reputation: 113

Difference between element.each and .each(element) in jquery

What is the difference between

$(element).each(function(){
});

And

$.each("element",function(){
});

Upvotes: 3

Views: 335

Answers (2)

Jonathan M
Jonathan M

Reputation: 17461

There is a difference. Per the docs:

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

Upvotes: 4

Related Questions