Javascript Window prompt() Method (original) (raw)

Last Updated : 23 Sep, 2024

The prompt() method in JavaScript displays a dialog box that prompts the user for input.

The **prompt() method returns the input value when the user clicks “OK” else returns null.

**Syntax:

prompt(message, default);

**Return value:

The prompt() method returns the text entered by the user in the input field as a string. If the user cancels the dialog box, null is returned.

**Example: This HTML code creates an interactive webpage featuring a “Click me!” button. Upon clicking, a prompt dialog appears asking for text input. If entered, the text is displayed on the webpage with a welcoming message. Canceling the prompt does nothing.

HTML `

Prompt() method

Window prompt() Method

<button onclick="geek()">
    Click me!
</button>
<p id="g"></p>

<script>
    function geek() {
        let doc = prompt(
            "Please enter some text",
            "GeeksforGeeks"
        );

        if (doc != null) {
            document.getElementById(
                "g"
            ).innerHTML =
                "Welcome to " + doc;
        }
    }
</script>

`

**Output:

WindowPrompt()

Javascript Window prompt() Method Example Output

We have a complete list of HTML DOM methods, to check those please go through the DOM Complete Reference article.

**Supported Browsers: