Execute a block of code based on user input using switch statement - Javascript Statement

Javascript examples for Statement:switch

Description

Execute a block of code based on user input using switch statement

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<input id="myInput" type="text">

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from w  ww  .j a v  a  2 s . c om*/
    var text;
    var fruits = document.getElementById("myInput").value;

    switch(fruits) {
        case "Banana": text = "Banana is good!";
                       break;
        case "Orange": text = "orange.";
                       break;
        case "Apple":  text = "apples";
                       break;
        default:
                       text = "unknown...";
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Related Tutorials