0

Please examine the switch statement

Only default option works.

case x="Fin" and case x="Intercom" are not working. Not sure why. Thanks!

Code:

 <p>Niche</p>
 <select name="niche" id="niche" onchange="FCM_niche()">
 <option value="empty" selected="">Select Niche</option>
 <option value="fin">FIN</option>
 <option value="intercom">Intercom</option>
 </select>
 <script type="text/javascript">
 function FCM_niche() {
 var x = document.getElementById("niche").value;
 switch (true) {
 case x="FIN":
 document.getElementById("FCM_select_niche").innerHTML = "FFFF";
 break;
 case x="Intercom":
 document.getElementById("FCM_select_niche").innerHTML = "IIII";
 break;
 default:
 document.getElementById("FCM_select_niche").innerHTML = "DDDD";
 }
 };
 </script>
<div id="FCM_select_niche"> 
</div>
Koby Douek
16.7k21 gold badges79 silver badges110 bronze badges
asked Mar 19, 2017 at 5:20
3
  • your switch statement is done wrong. switch (true) negates the whole point of having the switch in the first place. Commented Mar 19, 2017 at 5:22
  • @Bango - switch(true) is perfectly valid. But in the cases it should be == or ===, not =, i.e., it should be a comparison, not an assignment. (= isn't a syntax error, but it doesn't do what the OP wants.) Commented Mar 19, 2017 at 5:23
  • I'm not debating that it's valid. It just isnt how switch is meant to be used. Commented Mar 19, 2017 at 5:25

4 Answers 4

1

Please take a look at this JS fiddle showing the working version of your code: https://jsfiddle.net/jspruance/ebjnqt2r/

Just make a few small tweaks to your JS and you should be up and running.

1) Base your switch statement on the 'x' variable: 'switch(x)'

2) Base each case on the selected value (instead of the displayed text): ex: case "fin"

function FCM_niche() {
 var x = document.getElementById("niche").value;
 switch(x) {
 case "fin":
 document.getElementById("FCM_select_niche").innerHTML = "FFFF";
 break;
 case "intercom":
 document.getElementById("FCM_select_niche").innerHTML = "IIII";
 break;
 default:
 document.getElementById("FCM_select_niche").innerHTML = "DDDD";
 }
};

This should do the trick...good luck.

answered Mar 19, 2017 at 5:47
Sign up to request clarification or add additional context in comments.

Comments

1

Your switch statement is invalid. Also, you are checking switch case against selected text not on value

 <p>Niche</p>
 <select name="niche" id="niche" onchange="FCM_niche()">
 <option value="empty" selected="">Select Niche</option>
 <option value="fin">FIN</option>
 <option value="intercom">Intercom</option>
 </select>
 <script type="text/javascript">
 function FCM_niche() {
 var x = document.getElementById("niche").value;
 switch (x) {
 case "fin":
 document.getElementById("FCM_select_niche").innerHTML = "FFFF";
 break;
 case "intercom":
 document.getElementById("FCM_select_niche").innerHTML = "IIII";
 break;
 default:
 document.getElementById("FCM_select_niche").innerHTML = "DDDD";
 }
 };
 </script>
<div id="FCM_select_niche"> 
</div>

answered Mar 19, 2017 at 5:24

2 Comments

"Your switch statement is invalid." - What do you mean "invalid"? It is perfectly valid JS syntax that will run without error, it just doesn't do what the OP expects.
I mean the whole statement, not debating on the syntax @nnnnnn
1

Your switch case is wrong

This one is correct

function FCM_niche() {
 var x = document.getElementById("niche").value;
 switch (x) {
 case "fin":
 document.getElementById("FCM_select_niche").innerHTML = "FFFF";
 break;
 case "intercom":
 document.getElementById("FCM_select_niche").innerHTML = "IIII";
 break;
 default: document.getElementById("FCM_select_niche").innerHTML = "DDDD";
 break;
 }
 };

you need to just pass variable with switch(YOUR_VAR)

and just compare it with CASE "YOUR STRING":

Also javascript is case sensitive language , So FIN & fin is not equal value.

answered Mar 19, 2017 at 5:31

Comments

1
var x = document.getElementById("niche").value;
switch (x) {
 case "FIN":
 document.getElementById("FCM_select_niche").innerHTML = "FFFF";
 break;
 case "Intercom":
 document.getElementById("FCM_select_niche").innerHTML = "IIII";
 break;
 default:
 document.getElementById("FCM_select_niche").innerHTML = "DDDD";
}
answered Mar 19, 2017 at 5:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.