Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the difference between

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

And

$.each("element",function(){
});
like image 891
PHP Coder Avatar asked Nov 25 '25 17:11

PHP Coder


2 Answers

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.

like image 87
Jonathan M Avatar answered Nov 27 '25 06:11

Jonathan M


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

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

like image 35
Mohit Bumb Avatar answered Nov 27 '25 07:11

Mohit Bumb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!