Skip to main content

JavaScript- How to get input from user and display it on browser?

We can do that by using the prompt() method. We can display whatever the input given by the user on browser by using innerHTML.

for example:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Getting Input</title>
</head>
<body>
<button onclick="myFunction()">Click Here</button>
<p id="myName"></p>

<script>
    function myFunction() {
        var name = prompt("Please enter your name: ");        if(name != null){
            document.getElementById("myName").innerHTML = "Hello "+ name + ", How are you today?";        }

    }
</script>

</body>
</html>
 
This will display a "Click Here" button on browser and if you click on it, then it will ask you to enter your name like below:

 If the user enter his/her name and click OK, then it will display a paragraph like below:
Red marked area is the input given by the user.

Comments