I have two buttons inside my video projects, "Pause" and "Play" and looking for a special color effect to be applied on the current active button only, but clicking the current button couldn’t remove the color effect on the button which was clicked before.
The target is for the special color effect to be applied interchangeably. What line of code did I miss here?
const btns = document.querySelectorAll(".switch-btn");
btns.forEach((btn) => {
btn.addEventListener("click", function(event) {
const currentBtn = event.currentTarget;
if (!currentBtn.classList.contains("active")) {
currentBtn.classList.add("active")
} else {
currentBtn.classList.remove("active")
}
})
<div class="btns">
<button class="switch-btn" data-control="pause">Pause</button>
<button class="switch-btn" data-control="play"> Play</button>
</div>
2
Answers
First, remove the ‘active’ class from all buttons, Then add the ‘active’ class only to the clicked button, Just add the HTML, CSS and JS sequencing as per your HTML code: