1

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?

3 Answers 3

8

you can separate

document.getElementById(obj).style.fontWeight='bold';
document.getElementById(obj).style.color='#333';
Sign up to request clarification or add additional context in comments.

1 Comment

The property is fontWeight, not font-weight. You can't have hyphens in a script identifier.
6

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

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

3 Comments

I know a lot, but I never knew you could set cssText much the same way as innerHTML works. Nice to know.
Just wanted to add that this will reset any other styles you have associated with your element xx.
AFAIK only IE needs this syntax, all other browsers work by just setting the .style value.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.