JavaScript set input value by id | Simple HTML example

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 suggestion 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

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