Javascript Browser Window prompt() Method check user input

Introduction

Using the switch statement together with prompt() to execute a block of code based on user input:

Click the button to display a dialog box which will ask for your favorite drink.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>
<p id="demo"></p>
<script>
function myFunction() {/*from  www.  jav a2 s . c  o m*/
  var text;
  var favDrink = prompt("What's your favorite language?", "CSS");
  switch(favDrink) {
    case "CSS":
      text = "CSS it is";
      break;
    case "HTML":
      text = "HTML it is";
      break;
    case "Java":
      text = "Java it is?";
      break;
    default:
      text = "Default is Javascript";
  }
  document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>



PreviousNext

Related