Posted By

root_hacker on 04/12/11


Tagged

javascript best


Versions (?)

javascript: create DOM element without using for loop


 / Published in: JavaScript
 

when need to created a dom element repeatedly , use string concatenation to avoid for loop you can use repeat function from below.

  1. function repeat(str,n)
  2. {
  3. return new Array(n+1).join(str);
  4. }
  5.  
  6. repeat("<li class='empty'>" , 2);
  7.  
  8.  
  9. //output: "<li class='empty'><li class='empty'>"

Report this snippet  

You need to login to post a comment.