I'm trying to use getElementsByClassName, to hide HTML elements with onclick function. I'm using a for loop to iterate the array created by getElementsByClassName.
But this way I'm getting a: Uncaught TypeError: Cannot read property 'style' of undefined at HTMLDivElement.circles..onclick
Any ideas? Thanks a lot in advance.
<div class="circle" id="red-circle"></div>
<div class="circle" id="blue-circle"></div>
<div class="circle" id="yellow-circle"></div>
===============================================
const circles = document.getElementsByClassName("circle")
for(var i = 0; i < circles.length; i++) {
circles[i].onclick = () => {
circles[i].style.display = "none"
}
}