Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Page background color won't change.

Reply
Thread Tools

Page background color won't change.

 
 
test9991014@yahoo.com
Guest
Posts: n/a
 
      03-19-2008
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
 
 
 
 
Neredbojias
Guest
Posts: n/a
 
      03-19-2008
On 19 Mar 2008, wrote:

> 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?


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.

--
Neredbojias
http://www.neredbojias.com/
Great sights and sounds
 
Reply With Quote
 
 
 
 
test9991014@yahoo.com
Guest
Posts: n/a
 
      03-19-2008

> 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
 
test9991014@yahoo.com
Guest
Posts: n/a
 
      03-19-2008
Oops, I meant I did this

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

...and nothing changed.

 
Reply With Quote
 
test9991014@yahoo.com
Guest
Posts: n/a
 
      03-19-2008

This worked

var foo=document.getElementsByTagName("body")
foo[0].style.background="red"
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      03-19-2008
Scripsit :

> The body tag has no id, nor can I set one.


So whose document are you playing with, and why?

> 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
 
Jonathan N. Little
Guest
Posts: n/a
 
      03-19-2008
wrote:
> 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
 
Harlan Messinger
Guest
Posts: n/a
 
      03-19-2008
wrote:
> 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".

> 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
 
test9991014@yahoo.com
Guest
Posts: n/a
 
      03-19-2008

> 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
 
Eric B. Bednarz
Guest
Posts: n/a
 
      03-20-2008
"Jukka K. Korpela" <> writes:

> var body = document.getElementsByTagName("body")[0];


That's sort of silly. How many instances of the element type body would
you expect in a HTML document, by the way?

var body = document.body;

is shorter, better supported, usually faster, and part of the
HTMLDocument interface.

(not that I read alt.html, but anyway


--
||| hexadecimal EBB
o-o decimal 3771
--oOo--( )--oOo-- octal 7273
205 goodbye binary 111010111011
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing font color from current font color to black color Kamaljeet Saini Ruby 0 02-13-2009 04:58 PM
rounded corners in which my border color is different than the background color laredotornado@zipmail.com Javascript 1 02-14-2007 07:37 AM
Change Web Page background color programmatically VanReenenc ASP .Net 1 09-01-2006 09:12 PM
Changing Page Background Color Herb Stull ASP .Net 3 06-27-2006 02:40 PM
Problem with setting background color alternating item in datalist to a certain color fig000 ASP .Net Web Controls 0 09-06-2004 06:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57