Pass JavaScript variable to HTML input value | Example code

  • by

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

Input box

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

JavaScript code

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

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.