Generate div with class using jquery/javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Generate div with class using jquery/javascript?

I am trying to add 100 div elements with same class using js/jquery. I can't seem to make it work. Also is there a way to increment left margin of each element (first div with class="x" has a margin of 1px, the next div with class=x" should have a margin of 2px etc.) thank you!

12th Apr 2017, 7:29 PM
Mircea-Andrei Chițu
Mircea-Andrei Chițu - avatar
1 Answer
+ 10
Probabil te ajuta aceasta solutie care nu foloseste jquery :) var divs = []; for (var i=0; i<100; i++) { divs[i] = document.createElement("div"); divs[i].className = "myClass"; }
12th Apr 2017, 7:40 PM
Gami
Gami - avatar