How would I use forEach
to populate HTML elements? In my simple example below I'm trying to populate each DIV
with the counter "increment". I'm using this.innerHTML
to target each innerHTML in the array. I know I'm missing something but I can't see it.
var myArray = document.getElementsByClassName('box');
myArray = Array.from(myArray);
myArray.forEach( (item, increment) => this.innerHTML = increment );
body {
margin: 0;
padding: 0;
font-size: 2rem;
font-family: arial;
color: white;
}
.box {
margin: 0 0 10px;
padding: 10px;
background-color: blue;
}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>