Rewriting .each() loop as for loop to optimize, how to replicate $(this).attr()

Posted by John B on Stack Overflow See other posts from Stack Overflow or by John B
Published on 2012-12-12T17:00:58Z Indexed on 2012/12/12 17:03 UTC
Read the original article Hit count: 303

Filed under:
|
|

I running into some performance issues with a jquery script i wrote when running in ie so I'm going through it trying to optimize any way possible. Apparently using for loops is way faster than using the jQuery .each method. This has led me to a question regarding the equivalent of $(this) inside a for loop. I'm simplifying what I'm doing in my loop down to just using an attr() function as it gets across my main underlying question.

Im doing this with each(simplified)

var existing = $('#existing');
existing.each(function(){
console.log($(this).attr('id'));
});

And I've tried rewriting it as a for loop as such:

var existing = $('#existing');
for(var i = 0;i < existing.length;i++)
{
    console.log(existing[i].attr('id'));
}

Its throwing an error saying:

Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'   

Thanks.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about for-loop