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.


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

        var els=document.getElementById("in1");
        els.value = "Test Content"




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

Your email address will not be published. Required fields are marked *

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