'How do I convert this code so the functions can take a variable?
I am trying to change the color of nav bar elements when the mouse goes over them.
This piece of code does that but for only the first button:
let navOne = document.getElementById("nav1");
function mouseOn() {
nav1.style.color = "red";
}
function mouseOff() {
nav1.style.color = "black";
}
navOne.addEventListener('mouseover', mouseOn);
navOne.addEventListener('mouseout', mouseOff);
I have been trying to convert the code so the functions work for multiple buttons, but cannot seem to get it to work. Here is the code so far:
let navOne = document.getElementById("nav1");
function mouseOn(navButton) {
navButton.style.color = "red";
}
function mouseOff(navButton) {
navButton.style.color = "black";
}
navOne.addEventListener('mouseover', mouseOn(navOne));
navOne.addEventListener('mouseout', mouseOff(navOne));
It has no errors, but does not cause any color change when I move my mouse button over the nav1 element.
Any help is greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
