DEV Community

YuriSoma
YuriSoma

Posted on

Explain X Like I'm Five

Hello there,

I try to write a JS code that generates DIV elements with CSS classes in an HTML file. For example, if I want to make 50 DIV boxes, I just enter 50 and FOR loop will generate these 50 DIV boxes.
Currently, I have a code that gets the element by ID then makes innerHTML, but it's not correct as it generate DIV for one time then stopped.

  • Pls find the below code snippet:
var createDiv = "<div class='someClass' style='background-color: skyblue;' id='someID'>I'm BOX</div>";
document.getElementById("someElement").innerHTML = createDiv; /* This will show */
document.getElementById("someElement").innerHTML = createDiv; /* This will not show

Enter fullscreen mode Exit fullscreen mode

.
Above snippet only for example, also if I can print tiwse then I will make the FOR loop.

Help me pls with this. Thanks alot

Top comments (0)