Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing bgcolor with buttons 1

Status
Not open for further replies.

buspass01

Programmer
Nov 18, 2002
5
US
I am creating a page that the user can choose their background color by clicking a color button on the form. For some reason I can not get my buttons to work. Any suggestions? Please help

<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot;>
//HIDE FORM INCOMPATIBLE BROWSERS
var green_change = &quot;green&quot;;
var yellow_change = &quot;yellow&quot;;
var red_change= &quot;red&quot;;
function changeColor() {
if (document.colors.red_name==true){
document.bgColor=&quot;red&quot;;
}
else if (document.colors.yellow_name==true){
document.bgColor=&quot;yellow&quot;;
}
else if (document.colors.blue_name==true){
document.bgColor=&quot;blue&quot;;
}
}
</SCRIPT>
</head>
<body>
<form name=&quot;colors&quot; action=&quot;&quot; method=&quot;get&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;RED&quot; NAME=&quot;red_name&quot;onClick=&quot;changeColor()&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;YELLOW&quot; NAME=&quot;yellow_name&quot; onClick=&quot;changeColor()&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;BLUE&quot; NAME=&quot;blue_name&quot; onClick=&quot;changeColor()&quot;>
</form>
</body>
 
You will have style differences b/n IE and NetScape....

try this:

<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot;>
//HIDE FORM INCOMPATIBLE BROWSERS
var green_change = &quot;green&quot;;
var yellow_change = &quot;yellow&quot;;
var red_change= &quot;red&quot;;
function changeColor(inButton) {
document.bgColor = inButton.value}
</SCRIPT>
</head>
<body>
<form name=&quot;colors&quot; action=&quot;&quot; method=&quot;get&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;RED&quot; NAME=&quot;red_name&quot; onClick=&quot;changeColor(this)&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;YELLOW&quot; NAME=&quot;yellow_name&quot; onClick=&quot;changeColor(this)&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;BLUE&quot; NAME=&quot;blue_name&quot; onClick=&quot;changeColor(this)&quot;>
</form>
</body> -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Thanks mwolf00!!! That helps a lot!!!!


Does anyone know how to save the background color that the user picks in a cookie? So that when the user revisits the page the background color will the one that is saved in the cookie?

Thanks,

buspass01
 
<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot;>
//HIDE FORM INCOMPATIBLE BROWSERS
function setColor(){
pageColor = getCookie(&quot;color&quot;)
if (pageColor != &quot;not found&quot;){
document.bgColor = pageColor
}
}

function changeColor(inButton) {
document.bgColor = inButton.value
setCookie(&quot;color&quot;,inButton.value)
}
function setCookie(name, value){
document.cookie = name + &quot;=&quot; + value
}
function getCookie(name){
myCookie = &quot; &quot; + document.cookie + &quot;;&quot;
searchName = &quot; &quot; + name + &quot;=&quot;
nameLength = searchName.length
startOfCookie = myCookie.indexOf(searchName)

if (startOfCookie != -1){
startOfCookie += nameLength
endOfCookie = myCookie.indexOf(&quot;;&quot;, startOfCookie)
return myCookie.substring(startOfCookie,endOfCookie)
}
return &quot;not found&quot;
}
</SCRIPT>
</head>
<body onLoad=&quot;setColor()&quot;>
<form name=&quot;colors&quot; action=&quot;&quot; method=&quot;get&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;RED&quot; NAME=&quot;red_name&quot; onClick=&quot;changeColor(this)&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;YELLOW&quot; NAME=&quot;yellow_name&quot; onClick=&quot;changeColor(this)&quot;>
<INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;BLUE&quot; NAME=&quot;blue_name&quot; onClick=&quot;changeColor(this)&quot;>
</form>
</body> -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top