Make JavaScript take HTML input from user, parse and display?

The HTML input value is a string. To convert the string to integer, use parseInt().


Following is the code −

 Live Demo

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
   <input type="text" id="txtInput" placeholder="Enter a number">
   <button type="button" onclick="result();">GetANumber</button>
   function result() {
      var numberValue = document.getElementById("txtInput").value;
      if (!isNaN(numberValue))
         console.log("The value=" + parseInt(numberValue));
         console.log("Please enter the integer value..");

To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.


This will produce the following output on console −

Now, enter a value into the text box.

Now press the button, you will get the following output.

This will produce the following output −

The output in console −