I am trying to create multiple boxes from one box. But I am new to HTML, not know how to do it.
What I need:
I need one box; by clicking on it, two new boxes will create, and next time four boxes will create.
let elements = document.getElementsByClassName('box');
for(var i = 0; i < elements.length; i++)
{
console.log(elements[i]);
}
.box {
width: 100px;
height: 100px;
border: 1px solid green;
background: steelblue;
}
<div class="box"></div>
3
Answers
Here is my attempt (vanilla JS) :
I’m not sure of the best way to register a new element to an event so I made a function to register new boxes to
onClick
:boxToCreate
) boxesbox
classboxToCreate
by 2. Note that with your question we can’t tell if you want to increase the number of created each time by x2 or +2, you just need to changeboxesToCreate = boxesToCreate * 2
byboxesToCreate = boxesToCreate + 2
.You can try below code once I used
jQuery
for it.Here is code snippet:
This will increase boxes + 2 from previews generated
if you like you can change box_num += 2 to box_num *= 2 that will increase boxes * 2 from previerws generated