can you please help me with the following. I have a button which I want to change the color on click. After the first click, I want the color to change to "red". But then (when the button is already red), I want to change the color of the same button to "green".
Here's my code I'm trying to write, however, only the first part works (after the first click the button becomes red). How should I adjust it so that on the second click the button color changes to green? Thank you so much!
let element = document.querySelector("button");
function turnButtonRed () {
element.style.backgroundColor = 'red';
element.style.color = 'white';
element.innerHTML = 'Red Button';
element.onclick = turnButtonRed;
}
function turnButtonGreen(){
element.style.backgroundColor = 'green';
element.style.color = 'white';
element.innerHTML = 'Green Button';
}
function greenButton(){
if (element.innerHTML === 'Red Button'){
element.onclick = turnButtonGreen();
}
else {
element.onclick = turnButtonRed;
}
}
};