...

View Full Version : JavaScript button to change background colors



hull
03-09-2005, 04:57 PM
I am new to JavaScript. As a student we are given projects and I cannot for the life of me figure this one out. I am to use the a simple document object, specifically document.bgColor, to create one button that directs the user to click to change background color. I can change color fine but not to change the color back. The rub here is only to use one button and to keep it simple. If anyone can help me I would be eternally grateful. Thank You in advance

Kor
03-09-2005, 04:59 PM
can u show us what you have done so far? (we usualy don't do other's homework, but we might help you to correct it ;) )

hull
03-09-2005, 07:58 PM
Yes, I can show you what I have here at work I have some code at home that does not execute either. First I have this that works only one click.
now I tried to add some var but it did not execute. so I have left what does work and I feel I am just missing something. To correct an error It is document.bgColor sorry about that.

<HTML>
<head>
<title>color change once</title>
<script language="JavaScript">
<!--
function newbg (difcolor)
{
document.bgColor=difcolor;
}
//-->
</script>
</head>
<body bgColor="yellow">
<b>Stop looking at this horrible color! Click the button, Change the color.</b>
<p>
<input type="button" value="Change Color" OnClick="newbg('lightblue');">

</body>
</HTML>

Then I tried this:

<HTML>
<head>
<title>background Colors</title>
<script language="JavaScript">
<!--
function switchcolor()
{
if (document.bgColor=="yellow")
{
document.bgColor="lightblue";
}
else
{
if (document.bgColor=="lightblue")
{
document.bgColor="yellow";
}
}
}
//-->
</script>
</head>
<body bgColor="yellow">
<b>Want a new background color? Click the Button!</b>
<p>
<form>
<input type="button" value="Change Color" onClick="switchcolor();">
</form>

</body>
</HTML>

and this does not work at all. This is an intro course so I want to keep it simple. I am really just looking for suggestions as to what direction to move. I am new to JS and Forums too so any help is appreciated.

Hull



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum