Window prompt() Method - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window prompt


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

Parameter Values

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

Return Value:

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:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

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?";


Related Tutorials