What I want to achieve is to click one button and then the .clicked class will be added to the button I am clicking. But I also want to remove the class when I click one of the other buttons.
<div>
<button class="btn-color-black btn">Yo!</button>
<button class="btn-color-black btn">Yo!</button>
<button class="btn-color-black btn">Yo!</button>
</div>
Css:
.clicked {
color: pink;
}
Javascript
var btn = document.getElementsByClassName("btn");
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener("click", myFunction);
}
function myFunction() {
var parentElement = this.parentElement;
if (this.classList.length <= 2) {
this.classList.add("clicked");
} else {
this.classList.remove("clicked");
}
}