0

I have created a dynamic div which is rendered 19 times. I want to print the ID of iterator (i) in inner html dynamic Div. Can anyone please help me. Below is my code

var d1 = document.getElementById("div1");
for (var i = 0; i < 20; i++) {
  d1.innerHTML += '<div class="box">Print i here</div>';
}
<h2>My Loop Printing Divs</h2>
<div id="div1"></div>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
Mirza Bilal
  • 519
  • 1
  • 5
  • 15

2 Answers2

1

You can use variable directly like:

var d1 = document.getElementById("div1");
for (var i = 0; i < 20; i++) {
  d1.innerHTML += '<div class="box">Print ' + i + ' here</div>';
}
<h2>My Loop Printing Divs</h2>
<div id="div1"></div>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
0

you can used this way string ${js code} string

var d1 = document.getElementById("div1");
for (var i = 0; i < 20; i++) {
  d1.innerHTML += `<div class="box">Print ${i} here</div>`;
}
<h2>My Loop Printing Divs</h2>
<div id="div1"></div>