View Full Version : change background via javascript

05-30-2005, 05:00 PM
hey i want the viewers on my site to be able to change the background anyone who knows of a javascript wich is able to do this i would be very greatfull i have tried yahoo but no results were found!

thanks again codingforums! :thumbsup:

Coastal Web
05-30-2005, 06:19 PM
Do you want to change the background color?

more: http://www.google.com/search?hl=en&q=javascript+change+background+color

Or is it a background image you're trying to change?

more: http://www.google.com/search?hl=en&q=javascript+change+background+image

Samantha Gram

Single Paradox
05-30-2005, 07:04 PM
<a href="javascript:void(0);" onclick="document.body.style.backgroundColor='#000000';">Change BG to black</a>

^^ Change the "#000000" to whatever color you want the background to be.

05-30-2005, 07:49 PM
If you would like to do it more professionaly, something like this would work:

function changeBG(whatcolor){
document.body.style.backgroundColor = whatcolor;
document.body.backgroundColor = whatcolor;
<b>Like a form</b><br />
<form name="likeaform">
<input type="button" onclick="changeBG('black')" value="change to black" />
<input type="button" onclick="changeBG('blue')" value="change to blue" />
</form><br />
<b>Like a link</b><br />
<a href="#" onClick="changeBG('black');" />Change to Black</a>
<a href="#" onClick="changeBG('blue');" />Change to Blue</a>

This will work in Internet Explorer, Netscape Navigator, and I believe FireFox.

05-31-2005, 06:11 AM
and I believe FireFox.
No. You should use document.getElementById. All modern browsers support it.