I have a button and I want when every time I click on the button changes its color to another color with JavaScript
Asked
Active
Viewed 145 times
-6
-
Duplicate to https://stackoverflow.com/questions/1819878/changing-button-color-programmatically – rozalex Jun 23 '18 at 04:39
-
Possible duplicate of [Changing button color programmatically](https://stackoverflow.com/questions/1819878/changing-button-color-programmatically) – Zer0 Jun 23 '18 at 04:47
-
w3schools (https://www.w3schools.com/tags/tag_button.asp) has an article on how a button works and the functions of a button. – Assafi Cohen-Arazi Jun 23 '18 at 04:59
2 Answers
3
I believe the solution your looking for is the following:
document.getElementById("buttonID").onclick = function() {
document.getElementById("buttonID").style.backgroundColor = "yourColor";
}
This sample sets an onclick function to your button ID, and changes the background color. Of course, replace yourColor
and buttonID
with your appropriate needs.
Edit: I see that the question is a duplicate, and in the original question the answer you are looking for is further elaborated.

Assafi Cohen-Arazi
- 837
- 10
- 30
2
you have to do : HTML:
<button id="btn_id">BUTTON</button>
JS:
document.getElementById("btn_id").onclick = function() {
document.getElementById("btn_id").style.backgroundColor = "#000000";
}

გენო მუმლაძე
- 102
- 2
- 18