Changing background color defined in CSS stylesheet

2

2good2b

Is there a way to change the background color dynamically, when the
color is defined at a CSS stylesheet?
(tried document.bgcolor and this.style.background-color)
Thanks
 
R

Randy Webb

(e-mail address removed) said the following on 2/23/2007 7:47 PM:
Is there a way to change the background color dynamically, when the
color is defined at a CSS stylesheet?
(tried document.bgcolor and this.style.background-color)

document.body.style.backgroundColor="newColor"
Opera9 doesn't like it though.
 
2

2good2b

(e-mail address removed) wrote :
Is there a way to change the background color dynamically, when the
color is defined at a CSS stylesheet?
(tried document.bgcolor and this.style.background-color)
Thanks

Assuming this is your stylesheet declaration:

<style type="text/css">
body {color: black; background-color: white;}
</style>
and that you want to change the background-color to silver,

then:

if(document.styleSheets && document.styleSheets[0].cssRules)
// DOM 2 Stylesheets compliant
{
document.styleSheets[0].cssRules[0].style.backgroundColor = "silver";
}
else if(document.styleSheets && document.styleSheets[0].rules)
// IE specific
{
document.styleSheets[0].rules[0].style.backgroundColor = "silver";
};

W3C DOM 2 Style sheet: Attribute styleSheetshttp://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/stylesheets....

W3C DOM 2 CSS: Attribute cssRuleshttp://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/css.html#CSS...

Gérard


Worked like a charm, thank you Gérard!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top