I am trying to change the backgroundColor of the drumkey
class when clicking on a p
element. However, it is instantly applying when loading the page instead of onClick
.
var keyEl = document.getElementsByClassName("drumKey");
var x = document.getElementsByTagName("p");
for (i = 0; i < x.length; i++) {
x[i].addEventListener("click", colorChange(i));
}
function colorChange(i) {
keyEl[i].style.backgroundColor = "red";
}