Javascript examples for Statement:switch
Execute a block of code based on user input using switch statement
<!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>