JavaScript set input value by name | HTML example code

  • by

Use getElementsByName to get the elements and Set the value property. If multiple elements then Loop over the array and set each element’s value.

Example JavaScript set input value by name

HTML example code set input value programmatically using JS.

<html>
<body>

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

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

</body>

</html>

Output:

JavaScript set input value by name HTML

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.