Home > Enterprise >  How to change the color of a button when clicked, and when other button is clicked
How to change the color of a button when clicked, and when other button is clicked

Time:12-01

I have an html site with sections. On the top of the site, I have buttons with link to every section Default color: white. I want, when I click on a button to change it's color, but also, when I click on ANOTHER button, to change the color of the other button, and make the old button color white again.

I tried some script examples from stackoverflow, but I never find something working like what I wanted.

CodePudding user response:

you can use a code like this :

function changeFirstButtonColor() {
 document.getElementById("first-btn").style.backgroundcolor = 'new color';
 document.getElementById("second-btn").style.backgroundcolor = 'old color';
}

function changeSecondButtonColor() {
 document.getElementById("second-btn").style.backgroundcolor = 'new color';
 document.getElementById("first-btn").style.backgroundcolor = 'old color';
}

And call the function for the good button with onclick event

  • Related