Window prompt()

Example 1

Prompt for a user name and output a message:

let person = prompt("Please enter your name", "Harry Potter");

if (person != null) {
  document.getElementById("demo").innerHTML =
  "Hello " + person + "! How are you today?";
}
Try it Yourself »

More examples below.


Description

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

The prompt() method returns the input value if the user clicks "OK", otherwise it returns null.

Note

A prompt box is used if you want the user to input a value.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed.

Do not overuse this method. It prevents the user from accessing other parts of the page until the box is closed.

See Also:

The alert() Method

The confirm() Method


Syntax

prompt(text, defaultText)

Parameters

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

Return Value

Parameter Description
A string If the user clicks "OK", the input value is returned.
Otherwise null is returned.


More Examples

Prompt for his favourite drink:

let text;
let favDrink = prompt("What's your favorite cocktail drink?");
switch(favDrink) {
  case "Coca-Cola":
    text = "Excellent choice! Coca-Cola is good for your soul.";
    break;
  case "Pepsi":
    text = "Pepsi is my favorite too!";
    break;
  case "Sprite":
    text = "Really? Are you sure the Sprite is your favorite?";
    break;
  default:
    text = "I have never heard of that one!";
}
Try it Yourself »

Browser Support

prompt() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.