Javascript Reference - Window prompt() Method








The prompt() method displays a dialog box that prompts the visitor for input.

Browser Support

prompt Yes Yes Yes Yes Yes

Syntax

prompt(text, defaultText)

Parameter Values

Parameter Description
text Required. The text to display in the dialog box
defaultText Optional. The default input value




Return Value

Type Description
String If the user clicks 'OK', the input value is returned.
If the user clicks 'cancel', null is returned.

Example

The following code shows how to display a prompt box.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from   w  ww . java 2  s  . c  o  m-->
<p id="demo"></p>

<script>
function myFunction() {
    var person = prompt("Please enter your name", "Visitor");
    if (person != null) {
        document.getElementById("demo").innerHTML = "Hello " + person + "!";
    }
}
</script>

</body>
</html>

The code above is rendered as follows: