JavaScript > Functions with Parameters

Prompt box in JavaScript

How to show a prompt box in JavaScript?


If we want to get an input from user, prompt method can be used.

    <script>
        function PromptUser() {
            var yourName = prompt('What is your name?', '');
            if (yourName != null && yourName != "") {
                alert("Your name is [" + yourName + "]");
            }
        }
    </script>

    <input type="button" id="btnTest" onclick="PromptUser()" value="Click me!" />

Prompt method accepts two parameters, 1st is the message to prompt and 2nd is the default value you want to populate to the user.

OUTPUT

Above picture shows a sample Prompt. Once name is filled and clicked OK below alert will appear in the browser.

 Views: 5066 | Post Order: 24



Write for us