'How to change the background color of an li through a button and to use the same button to return the li to its original color?
everyone! I wrote the function below:
`function addClickEventToButton(buttonId, classReceiver) {
let buttonChangeColors = document.getElementById(buttonId);
let eventReceiver = document.getElementsByClassName(classReceiver);
buttonChangeColors.addEventListener('click', function () {
for (let i = 0; i < eventReceiver.length; i += 1) {
if (eventReceiver[i].style.backgroundColor = 'rgb(238,238,238)') {
eventReceiver[i].style.backgroundColor = 'white';
} else {
eventReceiver[i].style.backgroundColor = 'rgb(238,238,238)';
}
}
});`
The objective is to change the background color of 3 li's to white whenever I click a button. This part worked. The next thing I want to do is, when I click the same button again, to return their background color to the original color. Can anyone tell me what I'm doing wrong and how to fix it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
