tags:

views:

196

answers:

3

I'm trying to set the CSS style of an object with the following:

document.getElementById(obj).style='font-weight:bold; color:#333;';

but it's not working. Does anyone have any idea why?

+6  A: 

You need to set the underlying cssText of the style as folows:

document.getElementById('xx').style.cssText='font-weight:bold; color:#333;';
ZA
I know a lot, but I never knew you could set cssText much the same way as innerHTML works. Nice to know.
joebert
Just wanted to add that this will reset any other styles you have associated with your element xx.
Alec Smart
AFAIK only IE needs this syntax, all other browsers work by just setting the .style value.
scunliffe
+8  A: 

you can separate

document.getElementById(obj).style.fontWeight='bold';
document.getElementById(obj).style.color='#333';
Haim Evgi
The property is fontWeight, not font-weight. You can't have hyphens in a script identifier.
Guffa
Guffa: Corrected the typo.
furtelwart
A: 

Document's style property is a object, please reference HTML DOM Style Object on W3school

Liu Peng