HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Window prompt() Method

Window Object Reference Window Object

Example

Display a prompt box which ask the user for her/his name, and then write a greeting to the page:

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

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

Try it yourself »

Definition and Usage

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

A prompt box is often used if you want the user to input a value before entering a page.

Note: When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. Do not overuse this method, as it prevent the user from accessing other parts of the page until the box is closed.

The prompt() method returns the input value if the user clicks "OK". If the user clicks "cancel" the method returns null.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The prompt() method is supported in all major browsers.


Syntax

prompt(text,defaultText)

Parameter Values

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

Return Value

Type Description
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.


Window Object Reference Window Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE