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)
| Parameter | Description |
|---|---|
| msg | Optional. The plain text (rather than HTML formatted text) to be displayed in the dialog box. |
| 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, and 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 Yourself »
Window Object Other Extensions