473,594 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing background color defined in CSS stylesheet

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

Feb 24 '07 #1
3 5157
2g*****@gmail.c om 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.bgcolo r and this.style.back ground-color)
document.body.s tyle.background Color="newColor "
Opera9 doesn't like it though.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 24 '07 #2
2g*****@gmail.c om wrote :
Is there a way to change the background color dynamically, when the
color is defined at a CSS stylesheet?
(tried document.bgcolo r and this.style.back ground-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.sty leSheets && document.styleS heets[0].cssRules)
// DOM 2 Stylesheets compliant
{
document.styleS heets[0].cssRules[0].style.backgrou ndColor = "silver";
}
else if(document.sty leSheets && document.styleS heets[0].rules)
// IE specific
{
document.styleS heets[0].rules[0].style.backgrou ndColor = "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
On Feb 24, 10:38 pm, Gérard Talbot <newsblahgr...@ gtalbot.orgwrot e:
2goo...@gmail.c om wrote :
Is there a way to change the background color dynamically, when the
color is defined at a CSS stylesheet?
(tried document.bgcolo r and this.style.back ground-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.sty leSheets && document.styleS heets[0].cssRules)
// DOM 2 Stylesheets compliant
{
document.styleS heets[0].cssRules[0].style.backgrou ndColor = "silver";
}
else if(document.sty leSheets && document.styleS heets[0].rules)
// IE specific
{
document.styleS heets[0].rules[0].style.backgrou ndColor = "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5334
by: erik | last post by:
Is it posssible to inherit the previous pages (parent pages) background image? Is there a script out there I could look at? Thanks
2
2655
by: Martin | last post by:
Dear Experts! I'd be grateful if you can help me with this. I'm having an inline frame which points to 'www.externalsite.com'. The page which contains the inline frame has an orange background, the pages at 'www.externalsite.com' have a white background. Can I change the background of 'www.externalsite.com' to mach the background of the inline frame page upon loading?
27
13567
by: Kevin Yu | last post by:
When I declare on HTML page <LINK href="mycss.css" type="text/css" rel=stylesheet /> .... <BODY class=myclass> in mycss.css BODY { FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: Arial, Geneva; background-image: url(images/back.jpg); }
8
5036
by: Serge Hartmann | last post by:
hi there, I created two different backgrounds for the same page to obtain a (fake) left margin and a (true) right margin. -> left background, defined in body section, aligned bottom and with vertical repeat. -> right background, defined in html section, aligned top and without repeat it may look weird, but I don't think my problem is due to these margins.
25
4317
by: madsgormlarsen | last post by:
Hi I am making a tab menu with css, and the tabs changes color depending on wich tab is current. 1. I could load a different css file for each tab/color. 2. I could do some inline css only for the color of the tabs. (which I would then change with trough php). 3. I could make a different class for each color, and change the class of the tab.
2
2848
by: bissatch | last post by:
Hi, I am running a w3c CSS validation check on a site in development. I have many errors saying that my CSS is not valid because I have not defined the background-color but instead left it default transparent. Why does it require that every CSS defined element have their background-color defined? Also, when I set styles in the following way:
5
20146
by: 3Dfelix | last post by:
I have a problem when using a Master page file in diferent aspx files. Some of this aspx are in different places. I have see that the problem is with styles definition on master file, when use "background-image". In some aspx files this background image are lost. for example: background-image:url('../App_Themes/images/image2.gif '); Any solution for this?
2
2159
by: Jason_SanDiego2006 | last post by:
Hello all, Hang with me, I'm a little new. I'm working on a web application in C# using ASP.NET 2.0 The goal of my application is to have pages whose styles can be dynamically changed based on the logged-in user's saved preferences. The user can define their own styles (font-size, font-family, color, etc.)
9
2157
by: rods | last post by:
I have defined a css stylesheet and have a particular style defined as follows: .main_body_booked { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; font-style: normal; color: #000000; font-weight: bold ; text-decoration: line-through; background-color: #FFFF00} When I write some simple HTML code as follows: <span class="main_body_booked" >6</span> <img src="Images/booked.jpg" width="10" height="10">
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8231
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.