Instead of doing 12 modifications to the DOM, why not multiply the string and then use only one .append() call?
String.prototype.times = function(n) { return Array.prototype.join.call({length: n+1}, this); }; var star = '<div class="star" />'.times(12); $('body').append(star);
jsFiddle
Fix source method
If you want to stay with your method, you need to call the $ function inside the loop so that a separate jQuery object is created at each iteration. I guess this is slower, but it will work.
star = '<div class="star" />'; var body = $('body'); for ( i = 0; i < 12; i++ ) { body.append($(star)); }
jsFiddle
Jon gauthier
source share