JavaScript prompt dialog with example

The prompt dialog type

The prompt dialog is the way to ask a user for some input value in the form of a dialog box. User is given Ok and Cancel options to proceed after entering text. If Ok is pressed it will return entered value. If Cancel is pressed, the null is returned.

See a prompt dialog example

Syntax of using prompt box

This is how you can display javascript prompt dialog box:

prompt(“Message to be shown”, “default text in JS prompt box”);

You may enter default text inside JS prompt box.

Or use “window” prefix as follows:

window.prompt(“Message to be shown”, “default text in JS prompt box”);

Javascript dialog example with default value

Following is an example of javascript prompt dialog box. If a user enters some value this will be shown in a simple alert. See example by clicking the link below:

Experience this example online


Other dialog boxes

There are other javascript dialog boxes as well with simple OK or OK and Cancel options without prompt. Following are links to its chapters with examples.

Also see Javascript alert | Javascript confirm

Was this article helpful?

Related Articles

Leave A Comment?