Open In App

Javascript Window prompt() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Javascript  Window prompt() method is used to display a dialog box 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.

Return value:

The input value is returned if the user clicks the ‘OK’ button. If not so then null is returned.

Example: In this example, we will see the use of the Window.prompt() method. On clicking the button a prompt appears having a default value.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
 
    <title>Document</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() {
 
            let doc = prompt("Please enter some text",
                "GeeksforGeeks");
 
            if (doc != null) {
                document.getElementById("g").innerHTML =
                    "Welcome to " + doc;
            }
        }
    </script>
</body>
</html>


Output:

Output

Example 2: In this example, there is no default value in the prompt.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
 
    <title>Document</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() {
 
            let doc = prompt("Please enter some text");
 
            if (doc != null) {
                document.getElementById("g").innerHTML =
                    "Welcome to " + doc;
            }
        }
    </script>
</body>
 
</html>


Output:

promtttttttt

Output

We have a complete list of HTML DOM methods, to check those please go through the DOM Complete Reference article.

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 4
  • Firefox 1
  • Opera 3
  • Safari 1

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 15 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads