Use switch statement on string value - Javascript Statement

Javascript examples for Statement:Quiz

Description

Use switch statement on string value

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<input id="myInput" type="text" value="CSS">
<button onclick="checkFruit()">Check Fruit</button>
<p id="demo"></p>

<script>
function checkFruit() {/*from  w w w . j av a 2s  . c  o  m*/
  var text;
  var myArray = document.getElementById("myInput").value;

  switch(myArray) {
    case "Javascript":
      text = "good!";
      break;
    case "HTML":
      text = "OK";
      break;
    case "CSS":
      text = "yes";
      break;
    default:
      text = "Really?";
  }
  document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Related Tutorials