Using the Prompt Box in JavaScript

Using the prompt box in Java

The prompt box is used to input a value from the user. It contains a textbox, OK, and Cancel buttons. The user has to click either the OK or Cancel button to continue the execution of the code after entering the input value. If the OK button is clicked, the input value is returned. Otherwise, the box returns a null value.

Demo

  1. Create a document called promptBox.html.

    Prompt Box in JavaScript

  2. Write the following code,
    <!DOCTYPE HTML>
    <HTML>
    <HEAD>
    <TITLE>Using the prompt box</TITLE>
    </HEAD>
    <BODY>
    <H1>Accepting your name using promot box</H1> <SCRIPT type="text/javascript">
    var name prompt("Please enter your name: ""); if(name null name="")
    (
    name="visitor":
    }
    document.write("Hi! "+name+" Welcome to JavaScript");
    </SCRIPT>
    </BODY>
    </HTML>
  3. Execute the script by Opening the file in the Web Browser. If you are using Internet Explorer, click on “Allow Blocked Content” to allow the script to execute, and if you are using Mozilla Firefox, click on allow “ActiveX Controls”.

    Prompt Box in JavaScript

    When a user enters the name and hits the OK button,

    Prompt Box in JavaScript

    When the user clicks on the Cancel button.

    Prompt Box in JavaScript

Next Recommended Reading JavaScript Message Box