enter image description here[Picture of boxes]: https://i.stack.imgur.com/X018k.png
As shown in the image I have three boxes. What I want to do is when I click a box it turns the background-color green, then when I click it again it goes back to its original state. The code I have works, but is there a more efficient way of writing the JavaScript?
HTML
<div class="container">
<div class="box first"></div>
<div class="box second"></div>
<div class="box third"></div>
</div>
CSS
.colors {
background: green;
}
JavaScript
const first = document.querySelector(".first");
const second = document.querySelector(".second");
const third = document.querySelector(".third");
first.addEventListener("click", () => {
first.classList.toggle("colors");
});
second.addEventListener("click", () => {
second.classList.toggle("colors");
});
third.addEventListener("click", () => {
third.classList.toggle("colors");
});