views:

207

answers:

3

Dear All,
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?

Thanks,
Zsolt

A: 

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";
Nathan Campos
Sorry I put my reply to all posts two posts above, was to late yeterday :-(So just to repeat, none of the proposals work, although the last one I do not understand so I did not try it.Thanks for further hints.
Zsolt
A: 

I believe you want bgcolor. Something like this:

document.getElementById("button").bgcolor="#ffffff";

Here are a couple of demos that might help:

Background Color

Background Color Changer

DOK
+1  A: 

Probably best to change the className:

document.getElementById("button").className = 'button_color';

Then you add a buton style to the CSS where you can set the background color and anything else.

matpol
Remember to put the code highlight! **;)**
Nathan Campos
Thanks Nathan/DOK, none of these work. Simply nothing happens. Strange thing is that in CSS the color of the button is not defined at all. If I define it like color: #00cccc; then it changes (statically) the color of the label text..Dashcode is a bit specific I guess, it is Apple-ized Javascript...Any further idea?ThanksZsolt
Zsolt
might be something to with the id of the button being button - perhaps change the id to 'submit_button' or something
matpol
I changed the id right away, I have paranoia from reserved words...but it does not work.
Zsolt
do you have the class set up in the CSS?
matpol
Yes#my_button { font-family: HelveticaNeue-Bold; font-size: 12px; text-align: center; width: 60px; cursor: default; -khtml-user-select: none; position: absolute; right: auto; margin-top: 0px; top: 185px; bottom: 175px; height: auto; left: 109px; color: rgb(0, 204, 204);}sorry, do not know how to do code highlight...by default the color entry is not there, I added it by hand, and this changed the color of the text. I added it as #cc0000 but Daschode converts it into rgb()
Zsolt
The thing is that if I check the value of the document.getElementById("my_button").object.textElement.color then it picks up the newly set value, but this has no influence on visual appearance of the button itself, so it it either not .color or object.textElement.color.
Zsolt
you need to change the classname on document.getElementById("my_button") I don't think you need the object.textElement bit
matpol
sorry, I don't get it, can you point me to some URL where this is explained?
Zsolt
now I tried thisdocument.getElementById("my_button").setAttribute("class", "my_button2");where the my_button2 has a different color then my_button, also does not work :-(
Zsolt