Click to See Complete Forum and Search --> : CSS and JavaScript


AntonioMainenti
01-18-2003, 11:34 PM
How do I change the "background-color" property of a div with JavaScript?

document.all.the_div.style.background-color="black"

That doesn't work because it thinks you're subtracting color from document.all.the_div.style.background.

I also tried

document.all.the_div.style.bgcolor="black"

But it says that's not an object.

Please help. Thanks, Alan.

khalidali63
01-18-2003, 11:37 PM
the correct syntax is like this

document.getElementById("divId").style.backgroundColor="color";
hope this helps

Cheers

Khalid

AntonioMainenti
01-18-2003, 11:44 PM
I'm assuming this is designed to work in both Netscape and IE?

blufive
01-19-2003, 03:08 AM
Netscape 4.x will mess it up, and I suspect IE 4 will too, but Netscape 6+ and IE5+ should handle it just fine

AntonioMainenti
01-19-2003, 09:27 AM
Well, if it's not going to work in those browsers.. is there another way to write it that will?
Thanks, Alan.

khalidali63
01-19-2003, 03:06 PM
just perform a routine for browser versions
and then for ns4 use this

document.layers["divId"].style.backgroundColor="color";
for IE
document.all["divId"].style.backgroundColor="color";

I am just hoping that backgroundColor property was supported in ns4 and IE 4 browsers.

Khalid

gil davis
01-20-2003, 08:11 AM
Originally posted by AntonioMainenti
How do I change the "background-color" property of a div with JavaScript?The general rule is that if the CSS attribute has a "-" in it, then the JavaScript attribute drops the "-" and capitalizes the next letter. Thus "background-color" would become "backgroundColor".
Originally posted by khalidali63
document.layers["divId"].style.backgroundColor="color";NS 4 does not support the "style" object. Layers have a "bgColor", but no "style.backgroundColor".