I have a button on my page that when I mouse over I would like to change to the background color to red. I am getting this error:
Cannot read property 'addEventListener' of null
HTML
<button type="button" id="dev" onclick="visibility()">Visibility</button>
Javascript
var ditto = document.getElementById("dev");
ditto.addEventListener("mouseover", buttonColor);
function buttonColor(){
document.getElementById("dev").style.backgroundColor = "red";
}
here's what I thought I was doing:
created a variable for the specific element i wanted. Then add an eventlistener to that element for "mouseover" that would call the function buttonColor. buttonColor would change background color of button.
what did I do wrong?
edit: my js is an external file. I have to use the add event lisenter for a homework objective.