I am trying to toggle 5 circles (divs with class 'circle') on a page to change background color on a click with eventListener.
javascript:
var circle = document.querySelectorAll(".circle");
for(var i = 0; i < circle.length; i++){
circle[i].addEventListener('click', function(){
circle[i].classList.toggle('effect');
});
}
I keep getting the error "Cannot read property 'classList' of undefined" - I'm not sure why circle is undefined?
css:
.circle {
width: 100px;
height: 100px;
border: solid 3px black;
border-radius: 100%;
float: left;
margin: 1%;
}
.effect {
background-color: green;
border-color: blue;
}
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="circle">
</div>
<div class="circle">
</div>
<div class="circle">
</div>
<div class="circle">
</div>
<div class="circle">
</div>
<script src="script.js"></script>
</body>
</html>