JavaScript set input value by class name | Change value attribute example

Use getElementsByClassName return a list/array of elements. Pick element through index and set value on input element. You can also iterate over elements.

Use getElementById method if only one element needed to change value.

Example JavaScript set input value by class name

HTML example code :-

Output:

Example JavaScript set input value by class name

Another example

Loop over the array and set each element’s value. The getElementsByClassName() method returns a collection of an element’s child elements with the specified class name, as a NodeList object.

Do comment if you have any suggestions or doubts on this topic.

Note: 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.