HighDots Forums  

Page background color won't change.

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss Page background color won't change. in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
test9991014@yahoo.com
 
Posts: n/a

Default Page background color won't change. - 03-19-2008 , 08:38 AM






Hi all,

I'm altering a page whose background color being set in CSS
somewhere. But I need to change the background color
in Javascript. I tried doing so with this:

document.bgcolor = "red";
alert(document.bgcolor);

When run, the value is apparently set because the alert displays
"red", however the page itself does not change color.

Anybody know what's going on?

Thanks.

Reply With Quote
  #2  
Old   
test9991014@yahoo.com
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 09:36 AM







Quote:
Yes, but it isn't you... "document.bgcolor" is SO wrong it flabbergasts
me! Google for "document.getElementById" and the css setting "background"
for further information.
The body tag has no id, nor can I set one. However I did this

var foo = getelementbytagname("body")
foo.style.background = "red"

....and nothing changed.


Reply With Quote
  #3  
Old   
test9991014@yahoo.com
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 09:37 AM



Oops, I meant I did this

var foo = document.getelementbytagname("body")
foo.style.background = "red"

...and nothing changed.


Reply With Quote
  #4  
Old   
test9991014@yahoo.com
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 09:41 AM




This worked

var foo=document.getElementsByTagName("body")
foo[0].style.background="red"

Reply With Quote
  #5  
Old   
Jukka K. Korpela
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 09:51 AM



Scripsit test9991014 (AT) yahoo (DOT) com:

Quote:
The body tag has no id, nor can I set one.
So whose document are you playing with, and why?

Quote:
var foo = getelementbytagname("body")
foo.style.background = "red"
When everything else fails, read the ******* manual. In this case, start
from a primer on JavaScript, preferably a mordern one which is based on
W3C DOMs.

This is simple sample code, just to prove that you need to actually
study JavaScript before trying to use it:

var body = document.getElementsByTagName("body")[0];
body.style.backgroundColor = "red";

Note that JavaScript is case sensitive (and typically uses camelCase for
identifiers).

Beware that this code needs to be placed so that when it is executed,
the document, including the body element, has been loaded and parsed.

Followups trimmed. This is about JavaScript and DOM, not CSS, so both
group choices were wrong, but alt.html (a catchall group) is less wrong.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/



Reply With Quote
  #6  
Old   
Jonathan N. Little
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 09:52 AM



test9991014 (AT) yahoo (DOT) com wrote:
Quote:
This worked

var foo=document.getElementsByTagName("body")
foo[0].style.background="red"
Yes it helps when you

a) use the correct function name, yes it is case sensitive
b) take note of the return value

It "works" if client has JavaScript enabled.


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com


Reply With Quote
  #7  
Old   
Harlan Messinger
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 10:08 AM



test9991014 (AT) yahoo (DOT) com wrote:
Quote:
Hi all,

I'm altering a page whose background color being set in CSS
somewhere. But I need to change the background color in Javascript. I
tried doing so with this:

document.bgcolor = "red";
This doesn't alter any style. It doesn't do anything, in fact, other
than add a previously nonexistent field called "bgcolor" to the document
object and set its value to "red". Now, the BODY tag in transitional
HTML has an attribute called "bgcolor", but in Javascript, for whatever
reason, it's called document.bgColor, and names are case-sensitive in
Javascript.

Still, this has nothing to do with any properties that have been set
with CSS. A CSS property for an element is set in Javascript as follows:

var element = ...;
element.style.propertyName = "...";

The name of the CSS background color property in Javascript is
"backgroundColor".

Quote:
alert(document.bgcolor);

When run, the value is apparently set because the alert displays
"red", however the page itself does not change color.

Reply With Quote
  #8  
Old   
test9991014@yahoo.com
 
Posts: n/a

Default Re: Page background color won't change. - 03-19-2008 , 11:13 AM




Quote:
but in Javascript, for whatever
reason, it's called document.bgColor, and names are case-sensitive in
Javascript.
Thanks for that clarification.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.