SmokingMan Posted February 9, 2006 Share Posted February 9, 2006 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 More sharing options...
scott100 Posted February 9, 2006 Share Posted February 9, 2006 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 More sharing options...
SmokingMan Posted February 10, 2006 Author Share Posted February 10, 2006 So I guess I was trying to re-invent the wheel I'm teaching myself JS, and with no one here to talk to and ask questions of, it's slow going. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now