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);
- **message is a string of text to display to the user. It can be omitted if there is nothing to show in the prompt window i.e. it is optional.
- **default is a string containing the default value displayed in the text input field. It is also optional.
**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() methodWindow 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:
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.