Onclick should run a function and when clicked again should return/display the original value
When I clicked on a button I want it to run a function and when the buttons is clicked again I want it to go back to his default value. E.g. When I clicked on the button it should display "added to cart" when clicked again it should display "remove from cart" and the cycle continue. ///////////////The JavaScript code////// counter = 0; var number0fItems = document.getElementById("counter"); number0fItems.innerHTML = counter; /*this loops through the button to know which button was clicked*/ document.querySelectorAll(".eventBtn").forEach(function(item){ /*the var firstime is to stop the counter from iterating multiple times when the button is clicked multiple times*/ var firsttime = true; item.onclick = function(event){ if (firsttime){ firsttime = false; this.innerHTML = "Remove from cart"; this.style.backgroundColor = "#FFE9D6"; counter++; number0fItems.innerHTML = counter; } } })