Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't $.each() iterate through every item?

I have the following markup containing 10 pre elements with the class indent:

​<pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre> <pre class="indent"></pre>​ 

I'm using the following jQuery .each() function to iterate through each element:

​$(function(){         $.each(".indent", function(index){        alert(index);      });     });​ 

I would expect to see 10 alerts, however I only see 7

-- See Fiddle --


However, this works as expected with $(".indent").each():

$(function(){         $(".indent").each(function(index){        alert(index);      });     });​ 

-- See Fiddle --


Looking at the $.each() documentation, I understand theres a difference:

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object.

But I don't understand why in this instance, it doesn't iterate through all elements.

Why is this happening?

like image 597
Curtis Avatar asked Nov 30 '12 15:11

Curtis


People also ask

How do I run a forEach loop only in JavaScript?

There is, just put a break at the end of the block. But instead of doing this just get the one item you want out of the collection instead of "looping once". foreach traverses each element once it does not loop.

What line of code is used to iterate through all the properties?

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

Can you iterate array by using forEach loop How?

forEach() method iterates over the array items, in ascending order, without mutating the array. The first argument of forEach() is the callback function called for every item in the array. The second argument (optional) is the value of this set in the callback.


2 Answers

$.each(".indent", function(index){ 

doesn't iterate over the elements of $('.indent') but over the ".indent" string whose length is 7 chars.

See reference


A more detailed explanation based on jQuery source code :

jQuery first checks if the first parameter, obj (here your string), has a length :

var ...         length = obj.length,         isObj = length === undefined || jQuery.isFunction( obj ); 

Your string having a length (and not being a function), isObj is false.

In this case, the following code is executed :

for ( ; i < length; ) {     if ( callback.call( obj[ i ], i, obj[ i++ ] ) === false ) {         break;     } } 

So, given the function f, the following code

$.each(".indent", f); 

is equivalent to

for (var i=0; i<".indent".length; i++) {     var letter = ".indent"[i];     f.call(letter, i, letter); } 

(you can log the letters using var f = function(i,v){console.log(v)}; or be reminded one of the subtleties of call using var f = function(){console.log(this)};)

like image 72
Denys Séguret Avatar answered Oct 05 '22 19:10

Denys Séguret


You are iterating through the string, you should pass an object or an array to $.each method:

$(function(){         $.each($(".indent"), function(index){        alert(index);     });     }); 
like image 44
undefined Avatar answered Oct 05 '22 18:10

undefined