Reputation: 10976
I know I can use the jQuery .each()
function (and it works fine), but the performance is too sluggish. So I want to use a for loop and have the jQuery methods available inside it.
For example:
var anArray = $('#somediv').children(); // it has a lot of children
for(i=0;i<anArray.length;i++) { // or for(i=anArray.length;i--;)
anArray[i].addClass('sample_class'); // and a bunch of other stuff
};
Right now the browser throws this error:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'addClass'
Think it had something to do with the jQuery wrapper $()
I know I read the solution somewhere a while ago, but I'm just unable to retrace it.
Upvotes: 3
Views: 73
Reputation: 17380
You could also use the equivalent addClass
for Javascript
anArray[i].className = "sample_class";
Upvotes: 2
Reputation: 42166
Exactly , wrap it , because they are DOM elements and actually has no addClass
method :
$(anArray[i]).addClass('sample_class');
Reference: http://api.jquery.com/children/
Upvotes: 3