Javascript examples for Browser Object Model:Window prompt
Window prompt() Method - Use the switch statement with prompt() to execute a block of code based on user input:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w ww.j a va 2 s . c o m*/ var text; var favDrink = prompt("What's your favorite cocktail drink?", "A"); switch(favDrink) { case "B": text = "Excellent choice. B is good for your soul."; break; case "A": text = "A is my favorite too!"; break; case "C": text = "Really? Are you sure the C is your favorite?"; break; default: text = "I have never heard of that one.."; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>