I created nine blue div boxes and I'm trying to write a Javascript that will set the opacity of each box to 0 once the box is hovered over. I made an array of all the boxes but when I try to apply addEventListener to each array element using forEach method it returns "Cannot set properties of undefined" error. What mistake am I making?
const boxes=document.getElementsByClassName("box");
const arrayOfBoxes=Array.from(boxes);
arrayOfBoxes.forEach((box)=>{box.addEventListener("mouseover",hide)});
function hide(element){
element.style.opacity=0.0;
}
<div class="boxes">
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
<div class="box" style="background-color: blue; height:100px; width:100px; margin: 5px"></div>
</div>
NodeListimplementsforEach(), you don't needArray.from()