I want to use javascript to change the background colour within an IF statement?
2008-08-27 04:03:51 UTC
This is my code and its not working :(
Three answers:
sappy16
2008-08-27 04:39:13 UTC
hey. the first person who answered has it right - when comparing favcolor to a string in your if statements, you need to put the string in inverted commas (ie if (favcolor == 'purple'){//do this}. Otherwise, it thinks it is comparing to a variable called purple.
your browser should recognise the name of most common colours, so you could actually adjust your program so that the user can type almost any colour and the background colour will change. see the code below:
almost any colour you can think of is covered here: orange, blue, black, white, grey, purple, pink, red, yellow, green, even things like darkblue and darkgreen, crimson, dodgerblue, aliceblue, springgreen....
as long as there's no space between two words, that is!
here's a list of all the named colours:
http://www.gb.nrao.edu/~cbignell/testcolors.html
lauri h
2008-08-27 04:20:19 UTC
Why Ur previous code didn't work? Because statement if(favcolor==yellow) means there are variable yellow (like yellow="#ff0000" or yellow = false), but u need to compare favcolor to string like "yellow"
2016-12-12 14:04:17 UTC
HTML HEAD 6f9dccd85b2e786c8d52245365eb48JavaScript Test6f9dccd85b2e786c8d52245365eb48 HEAD -->