The prompt()
method displays a dialog box that prompts the visitor for input.
prompt |
Yes | Yes | Yes | Yes | Yes |
prompt(text, defaultText)
Parameter | Description |
---|---|
text | Required. The text to display in the dialog box |
defaultText | Optional. The default input value |
Type | Description |
---|---|
String | If the user clicks 'OK', the input value is returned. If the user clicks 'cancel', null is returned. |
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: