-1

Quick question, how do I take the below box:

HTML

<div class="box">
I am a box.
</div>

CSS

.box {
border: 1px solid green;
}

JavaScript

let x = 

and make it appear on my page 4 times?

https://jsfiddle.net/W3Develops/s6v8zafm/3/

mplungjan
  • 169,008
  • 28
  • 173
  • 236
w3Develops
  • 358
  • 1
  • 3
  • 15

1 Answers1

1

You could do something like this:

let box = document.querySelector('.box');
for(let x = 0; x < 3; x++) {
    let clone = box.cloneNode(true);
    document.body.appendChild(clone)
}

Fiddle link: https://jsfiddle.net/tdx5f6cw/6/

Anton Bks
  • 482
  • 3
  • 10
  • Mine https://jsfiddle.net/mplungjan/go9vL76w/10/ ;) – mplungjan Aug 19 '19 at 08:58
  • Thank you Anton this is the perfect answer to my question. I went to the link the community said this was a duplicate of and oh my gosh I was way over my head reading through that documentation, haha I was abut to go back to Google. Thanks again you totally saved the day! – w3Develops Aug 19 '19 at 09:00
  • @mplungjan even better, more readable with maxBox and numBox :D – Anton Bks Aug 19 '19 at 09:03
  • @JonathanJackson ah my bad I cloned it, guess I need to republish it to make it visible, anyways good luck! – Anton Bks Aug 19 '19 at 09:05
  • Here is the dupe code. It is pretty straight forward `function cloning() { var container = document.getElementById('mydiv'); var clone = document.getElementById('div_0').cloneNode(true); clone.setAttribute('id','div_'+document.getElementById('mydiv').getElementsByTagName('div').length); container.appendChild (clone); }` - change container to body and you do not need the line with the ID since you do not have an ID – mplungjan Aug 19 '19 at 09:11
  • @mrplungjan this looks very interesting, I'm about to start Googling some of the terms here that look familiar but that I do not exactly know to gain a better understanding. Thank you very much. – w3Develops Aug 19 '19 at 09:33