Javascript examples for Browser Object Model:Window prompt
The prompt() method displays a dialog box that prompts the visitor for input.
Parameter | Type | Description |
---|---|---|
text | String | Required. The text to display in the dialog box |
defaultText | String | Optional. The default input text |
A String.
If the user clicks "OK", the input value is returned.
If the user clicks "cancel", null is returned.
If the user clicks OK without entering any text, an empty string is returned.
The following code shows how to Display a prompt box which ask the user for her/his name, and output a message:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from ww w . ja v a2s . c o m*/ var person = prompt("Please enter your name", "Mary"); if (person != null) { document.getElementById("demo").innerHTML = "Hello " + person + "! How are you today?"; } } </script> </body> </html>