0

I'm trying to use for..in loop to select the objects within a variable and it doesn't show up the necessary Li objects.

var mUlLi = $(mUl).find('>li');      //select all li's in main Ul   
var listLength = $(mUlLi).length;
if(listLength >0 ){
    /*for(i=0;i <listLength; i++) {
        console.log(mUlLi[i]);   // works fine show the li objects  
    }*/
    for(var obj in mUlLi) {
        console.log(obj);   // show's diff objects
    }
}

How can i fix this ?

2
  • The DOM objects in a jQuery object are NOT properties of the jQuery object that can be iterated with for/in. Commented May 11, 2012 at 6:07
  • Meh, always nice to see an answer getting accepted that was not only posted later but also didn't contain the answer the OP wanted for most of the time. Btw, @user1184100, you should really change your nick. Commented May 11, 2012 at 6:22

4 Answers 4

5

jQuery has an each() that does the same thing.

$(mUl).find('>li').each(function(){ //for each of the elements found
    console.log(this);              //in here, "this" is the DOM element <li>
});

If you used a for in on a jQuery object, you'd also be looping through the jQuery methods and properties.

However, if you really want to do a for loop on the elements you got from that jQuery (because you didn't want to use each()), then do it directly:

var nodes = $(mUl).find('>li'),
    nodesLength = nodes.length,
    i, node;

for(i=0,i<nodesLength;i++){
    node = nodes[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

No need to extract the array first - jQuery objects support [], too.
2

mUlLi (problematic variable name) is not a regular object, is a jQuery collection. You can iterate with each().

mUlLi.each(function(){
   // `$(this)` is the current jQuery element
})

Comments

2

How about using jQuery's each function?

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

$(mUl).find('>li').each(function(i,v) {
    console.log(v);
});

Comments

2

You can fix this by using the proper way to iterate over an array - for(.. in ..) is NOT meant for iterating over array elements/indexes but for object properties - which is not what you want here.

Simply use the jQuery way via .each():

mUlLi.each(function() {
    console.log(this);
});

If you do not want this for some reason (probably not a valid reason!), you could also use a good old for loop:

for(var i = 0; i < listLength; i++) {
    var elem = mUlLi[i];
    console.log(elem);
}

7 Comments

Why not? That's the way to go here. But see the second code block in my answer for this case.
each() is slower than for..in and i have 50 diff elements
The performance difference is really negligible! And 50 elements are not that much anyway. Why do you think everyone else uses .each() if it's sooooo slow?
yeah true :) but still wanted to try for..in ;)
sounds to me like you're just to new to jQuery to admit you didn't know about the .each function. Always remember, the quickest path to ignorance is to deny it exist in one's self.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.