Display a prompt box which ask the user for her/his name, and output a message:
Click the button to demonstrate the prompt box.
<!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 person = prompt("Please enter your name", "Harry Potter"); if (person != null) { document.getElementById("demo").innerHTML = "Hello " + person + "! How are you today?"; } } </script> </body> </html>
The prompt()
method displays a dialog box that prompts users for input.
The prompt()
method returns the input value if the user clicks "OK".
If the user clicks "cancel" the method returns null.
prompt(text, defaultText);
Parameter Values
Parameter | Type | Description |
---|---|---|
text | String | Required. The text to display in the dialog box |
defaultText | String | Optional. The default input text |
If the user clicks OK without entering any text, an empty string is returned.