I have a simple form drop-down, and I want to display something different depending on the selection value. I have a variable called connectiontype that comes in wi
You are using =
(assignment) instead of ==
(comparison).
if (connectiontype == 'red') {
...
} else if (connectiontype == 'green') {
...
}
When you have an assignment, eg: lhs = rhs
the whole expression returns whatever rhs
is. So:
if (connectiontype = 'red') { ...
// is equivalent to (as far as the "if" is concerned):
if ('red') { ...
Since 'red'
(a non-empty string) is "truthy" in JavaScript, the if
is always true and your html
variable will always be set to 'Red'
.