Javascript | Window prompt() Method
The prompt() method is used to display a dialog with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page.
It returns a string containing the text entered by the user, or null.
Syntax:
prompt(message, default)
- message is a string of text to display to the user.It can be omitted if there is nothing to show in the prompt window i.e. it is optional.
- default is a string containing the default value displayed in the text input field. It is also optional.
Example:
<!DOCTYPE html> < html > < head > < title > Window prompt() Method </ title > </ head > < body style = "text-align: center;" > < h1 style = "color:green;" > GeeksforGeeks </ h1 > < h2 > Window prompt() Method </ h2 > < button onclick = "geek()" > Click me! </ button > < p id = "g" ></ p > < script > function geek() { var doc = prompt("Please enter some text", "GeeksforGeeks"); if (doc != null) { document.getElementById("g").innerHTML = "Welcome to " + doc; } } </ script > </ body > </ html > |
Output:
Before clicking the button:
After clicking the Click me! button:
After pressing the OK button in dialog box:
Supported Browsers: The browser supported by window prompt() method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- Javascript | Window Open() & Window Close Method
- Javascript | Window Blur() and Window Focus() Method
- JavaScript | Window getComputedStyle() Method
- Javascript | Window confirm() Method
- JavaScript | Window print() Method
- Javascript | Window scrollTo() Method
- JavaScript Course | JavaScript Prompt Example
- Window | Window.requestAnimationFrame() Method
- JavaScript BOM | Window Screen
- JavaScript | Window innerWidth and innerHeight Properties
- How to close current tab in a browser window using JavaScript?
- Cross-browser window resize event using JavaScript/jQuery
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- How to set font size based on window size using JavaScript ?
- HTML | DOM Window btoa() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vishal Chaudhary 2