I am struggling with a homework assignment and don't understand why I am receiving an error message saying 'undefined is not an object (evaluating 'buttons[i].style')). Any help would be appreciated. I have been attempting to loop through the buttons array and add an Event Listener to each item in the list.
var buttons = document.getElementsByClassName("quality");
for(var i = 0; i < buttons.length; i += 1){
buttons[i].addEventListener('click', function(i) {
buttons[i].style.background = "red";
});
}
<!doctype html>
<html>
<head>
<title>L.A. Hiking</title>
<link rel="stylesheet" href="css/hiking.css">
</head>
<ul id = "navbar">
<li><a href="index.html" class= "selected">Home</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="about.html">About</a></li>
</ul>
<body>
<div>
<div class="blocks" id="selections">
<ul id= "attr1">
<li class="Instruction">Pick one:</li>
<li class ="quality">Hard</li>
<li class ="quality">Medium</li>
<li class ="quality">Easy</li>
</ul>
<ul id = "attr2">
<li class="Instruction">Pick one:</li>
<li class ="quality">Hard</li>
<li class ="quality">Medium</li>
<li class ="quality">Easy</li>
</ul>
<ul id = "attr3">
<li class="Instruction">Pick one:</li>
<li class ="quality">Hard</li>
<li class ="quality">Medium</li>
<li class ="quality">Easy</li>
</ul>
</div>
<div class="blocks" id="results">
<ul id = "choices">
<li class="choice">Pick one:</li>
<li class ="choice">Hard</li>
<li class ="choice">Medium</li>
</ul>
</div>
</div>
<div id="button-container">
<button>Go!</button>
</div>
<script src="javascript/hiking.js"></script>
</body>
</html>