Skip to content

Pass JavaScript variable to HTML input value | Example code

Using getElementById attribute you can Pass JavaScript variable to HTML input value.

Input box

<input type="text" id="mytext">

JavaScript code

document.getElementById("mytext").value = msg;

Pass variable to HTML input value using JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <input type="text" id="mytext">

  <script type="text/javascript">

    var msg = "Hello World!";
    document.getElementById("mytext").value = msg;

  </script>

</body>
</html>

Output:

Pass JavaScript variable to HTML input value

Do comment if you have any doubts or suggestions on this JS variable topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

1 thought on “Pass JavaScript variable to HTML input value | Example code”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading