Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing button color programmatically

Is there a way to change the color of a button, or at least the color of the button label programmatically? I can change the label itself with

document.getElementById("button").object.textElement.innerText = "newlabel"; 

But how to change the color?

like image 988
Zsolt Avatar asked Nov 30 '09 13:11

Zsolt


People also ask

How do you change the color of a button using JavaScript?

</head> <body> <input type="button" onmouseover="ChangeColor()" value="Button" id="btn1" />

How do I change the color of a button in HTML?

All style elements in your HTML button tag should be placed within quotation marks. Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:".

How do you color a button in Java?

Use the setBackground method to set the background and setForeground to change the colour of your text.

How can change background color of button in Android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


2 Answers

I have finally found a working code - try this:

document.getElementById("button").style.background='#000000'; 
like image 124
Maurizio Pozzobon Avatar answered Oct 26 '22 03:10

Maurizio Pozzobon


Here is an example using HTML:

<input type="button" value="click me" onclick="this.style.color='#000000'; this.style.backgroundColor = '#ffffff'" /> 

And here is an example using JavaScript:

document.getElementById("button").bgcolor="#Insert Color Here"; 
like image 24
Nathan Campos Avatar answered Oct 26 '22 04:10

Nathan Campos