[![enter image description here][1]][1]I wanted to create a simple app for drawing by coloring the background of squares by clicking on them. I added a click event on every li which changes the background-color by adding a class. However, I'd like to take it to the next level by adding 4 buttons that would change the colors of the hightlight. Can anyone help me with that?
So my problem is: How to click a button (li in the header) that will select the color for my original event listener on ul class=grid li?
<header>
<ul class="header">
<li class="black">Black</li>
<li class="red">Red</li>
<li class="blue">Blue</li>
<li class="orange">Orange</li>
</ul>
</header>
<ul class="grid">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
.......
const myElements = document.getElementsByTagName("li");
for (let i = 0; i < myElements.length; i++) {
myElements[i].addEventListener("click", function () {
let selected = myElements[i];
selected.classList.toggle("highlight");
});
}
I tried (remove and add new class, replace class, toggle)
black.addEventListener("click", function () {
selected.classList.replace("highlight", "black");
});
But the change of background color is only one-off. I want to new class become permanent. 1]: https://i.stack.imgur.com/Fgg87.png