What is the difference between $.each(selector) and $(selector).each()
Description:
.each
is an iterator that is used to iterate over only jQuery objects collection whilejQuery.each
($.each
) is a general function for iterating over JavaScript objects and arrays.
Examples
1) Using $.each()
function
var myArray = [10,20,30];$.each( myArray, function(index, value) { console.log('element at index ' + index + ' is ' + value);});//Outputelement at index 0 is 10element at index 1 is 20element at index 2 is 30
2) Using .each()
method
$('#dv').children().each(function(index, element) { console.log('element at index ' + index + 'is ' + (this.tagName)); console.log('current element as dom object:' + element); console.log('current element as jQuery object:' + $(this));});//Outputelement at index 0 is inputelement at index 1 is pelement at index 2 is span
Resources
from http://api.jquery.com/jQuery.each:
The $.each() function is not the same as .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.