Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to change the background color of my site with the click of a menu button. I've got the code to work for a Div however it wont let me access body.

XML
<div id="test">
<a href="#" onclick="document.getElementById('body').style.background = '#238902'" />GREEN</a></li>
</div>


It simply throws up the error 'Object required' and links the URL.
Any suggestions?
Posted

Try this:

XML
<div id="test">
<a href="#" onclick="document.body.style.background = '#238902'" />GREEN</a></li>
</div>


Edit: Body is not an element unless you decide to put an ID='body' in the <body> tag.
 
Share this answer
 
v2
Comments
WurmInfinity 14-Jan-12 12:30pm    
to obvious eh... THANKS :D
Sergey Alexandrovich Kryukov 14-Jan-12 15:18pm    
Did you test it? Think again... :-)
--SA
Sergey Alexandrovich Kryukov 14-Jan-12 15:18pm    
Please, you need to test your solutions before posting an answer, otherwise you waste members' time. It does not work. Please see my answer for a correction.
--SA
WurmInfinity 14-Jan-12 15:19pm    
See my reply to yours. His does work.
jinxster 14-Jan-12 20:52pm    
I like to set the base word because if you decide to add an image then you can just add url(../images/image.png) in front without creating a new line.
The answer by jinxster is not accurate, background property is not correctly spelled.

You need to do something like this:
JavaScript
document.body.style.backgroundColor = 'red';


—SA
 
Share this answer
 
Comments
WurmInfinity 14-Jan-12 15:17pm    
Actually no his did work, and for some reason when your applying the style in that manner you simply declare the base word. For example if you wish to change font size you do style.font as opposed to font-size. Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900