shampouya
asked on
How do I change the body background color with an input button?
I can't figure out why none of these three attempts are changing the background color of my webpage linked below. Does anyone know why?
http://wdiclass.com/poureb/homework4special/homework4special.html
<input type="button" onClick="document.body.style.background-color='lavender';" value="Change background color" />
<ul>
<li><img src="lavenderSquare.jpg" onClick="return backgroundLavender()"></img></li>
<li><a href="#" onClick="javascript:changeBGC('#000099')">Click Blue</a></li>
</ul>
http://wdiclass.com/poureb/homework4special/homework4special.html
you can just use this document.bgColor if you want to change the page color see below
<input type="button" onClick="document.bgColor= 'lavender' ;" value="Change background color" />
<input type="button" onClick="document.bgColor=
ASKER
I see, and how come this separate function doesn't work?
<script>
function backgroundLavender(){
document.bgColor="lavender";
}
</script>
<ul>
<li><img src="lavenderSquare.jpg" onClick="backgroundLavender()"></img></li>
</ul>
It should work , it is working for me .
ASKER
So when you click on the lavender square image, the body background turns lavender?
Yes , which browser are you using ?
ASKER
Using firefox 8.0. I am able to change the background with the "Change Background Color" button, but not with with the small purple square image. Any idea why the purple square does nothing?
You can do it in diff ways
1)
<input type="button" onClick="document.bgColor= 'lavender' ;" value="Change background color" />
2)
function changeBackground() {
document.body.style.backgr ound = 'lavender';
}
3)
Write a css func
<style>
body.lavbg
{
background: lavender;
}
</style>
<input type="button" onClick="document.body.cla ssName='la vbg';" value="Change background color" />
1)
<input type="button" onClick="document.bgColor=
2)
function changeBackground() {
document.body.style.backgr
}
3)
Write a css func
<style>
body.lavbg
{
background: lavender;
}
</style>
<input type="button" onClick="document.body.cla
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually, this worked:
function changeBackground() {
document.body.style.backgr ound = 'lavender';
}
function changeBackground() {
document.body.style.backgr
}
ASKER
You're right
ASKER
thanks
So...
document.body.style.backgr