Window prompt() method
Window Object
Definition and Usage
The prompt() method is used to display a dialog box that prompts the user for input.
This method returns the string entered by the user.
Syntax
prompt(
msg,defaultText
)
| Parameters | Description |
|---|---|
| msg | Optional. The plain text to display in the dialog box (not HTML-formatted text). |
| defaultText | Optional. The default input text. |
Browser support





All major browsers support the prompt() method
Example
Example
Display a prompt box asking for his/her name, then write a greeting on the page:
function
myFunction
(
)
{
var
x
;
var
person
=
prompt
(
"
Please enter your name
"
,
"
Harry Potter
"
)
;
if
(
person
!=
null
&&
person
!=
"
"
)
{
x
=
"
Hello
"
person
"
! How are you feeling today?
"
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
x
;
}
}
Try it »
Window Object