Skip to content

JavaScript set input value by id | Simple HTML example

  • by

Use id and getElementById method to Change the value of a text field using JavaScript.

This is one way of doing it:

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

Example JavaScript set input value by id

HTML Example code Set the value of an input field using JS:-

You can write the code in JavaScript as given below to set its value as.

<html>
<body>

    <input id="in1" type="text" >
    <script>

        var els=document.getElementById("in1");
        els.value = "Test Content"
    </script>

</body>

</html>

Output:

Example JavaScript set input value by id

Do comment if you have any doubts and suggestions on this 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

Discover more from Tutorial

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

Continue reading