0

I made somesimple js code but result is unexpected. how this is possible?

var n = $("#mGrid tbody tr[class*='success']");
console.log(n.length);

for (var i in n)
{
    console.log("x");
}

console

2

202 x

1
  • You can just use .each() to loop through all the DOM nodes selected by n. Commented Apr 23, 2016 at 11:25

1 Answer 1

1

That means 202 times x was printed in the console. When you are using a for..in loop over an object, the enumerable property of the particular object will be iterated until the end of prototype chain of it.

Your object has 202 numerable properties both own and prototype properties.

For iterating over the jquery object, you can use .each like below,

e.each(function(){
 //$(this) the current element on the iteration.
});

Still if you want to use a for loop, then you have to do like below,

for (var i=0,i< n.length;i++) {
    console.log("x");
}
Sign up to request clarification or add additional context in comments.

2 Comments

how can I iterate tr classes?
@Mert You can use the .each approach that I given above.

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.