By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Changing background color defined in CSS stylesheet

P: n/a
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

Feb 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
2g*****@gmail.com 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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 24 '07 #2

P: n/a
2g*****@gmail.com 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 styleSheets
http://www.w3.org/TR/2000/REC-DOM-Le...-DocumentStyle

W3C DOM 2 CSS: Attribute cssRules
http://www.w3.org/TR/2000/REC-DOM-Le...Sheet-cssRules

Gérard
--
Using Web Standards in your Web Pages (Updated Dec. 2006)
http://developer.mozilla.org/en/docs...your_Web_Pages
Feb 24 '07 #3

P: n/a
On Feb 24, 10:38 pm, Gérard Talbot <newsblahgr...@gtalbot.orgwrote:
2goo...@gmail.com 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
--
Using Web Standards in your Web Pages (Updated Dec. 2006)http://developer..mozilla.org/en/doc...s_in_your_Web_...

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

Feb 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.