Jump to content

Using script to change page color


SmokingMan

Recommended Posts

I'm trying in this exercise to change the color of a page according to a drop down selection. But no matter what is selected, the page always goes to the same color. I know I've probably missed something simple, but I'm still learning JS and it's just not jumping out at me. Here's the code:

<title>Chapter 9 Exercise 5</title><script language="javascript"><!--function changeBgColor(list)	{	color = list.options[list.selectedIndex].value	// assemble data for new document color	var newColor = "<html><head><title>New Color Document</title></head>"	newColor += "<body bgcolor='color'><br><b>Click the back button for original window"	newColor += "</b></body></html>"	// write new document	document.write(newColor)	document.close() // end rewrite }--></script></head><body><form>Select a background color:<select name="colorList" onChange="changeBgColor(this)">	<option selected value="red">Stop</option>	<option value="yellow">Caution</option>	<option value="green">Go</option></select></form></body>

What am I missing here? As I have the variable 'color' defined, would it not contain the value selected in the dropdown list? I tried doing this without having the first option selected, but there's no difference. The new page is always red.

Link to comment
Share on other sites

I just took out the code that wrote a new html page and replaced with document.bgColor=color;Works a treat now :)

<title>Chapter 9 Exercise 5</title><script language="javascript"><!--function changeBgColor(list) {color = list.options[list.selectedIndex].valuedocument.bgColor=color;}--></script></head><body><form>Select a background color:<select name="colorList" onChange="changeBgColor(this)"><option selected value="red">Stop</option><option value="yellow">Caution</option><option value="green">Go</option></select></form></body>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...