I am looking to create click events that can toggle a button's text between "on" and "off" states indefinitely. Currently, when I click the button once, the text changes to "Reset Filter," and on clicking again, it reverts back to "Filter" due to the nested event listener. However, I want to be able to continuously switch between these two states.
Despite attempting while loops and other methods, I have realized that this might require an asynchronous approach.
const filterBtn = document.getElementById('filter-btn');
// Event Listener to activate filter button
filterBtn.addEventListener('click', () => {
if (filterBtn.textContent === "Filter") {
filterBtn.textContent = "Reset Filter";
} else {
filterBtn.textContent = "Filter";
}
});
<button id="filter-btn">Filter</button>
I am seeking a solution that allows for seamless switching between the "on" and "off" states of the button.