javascript: create DOM element without using for loop


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.