JavaScript get element by name Method | Example code

Use getElementsByName() method t0 get get element by name in JavaScript. This method returns an NodeList of elements. Because it’s plural in this method.

That returns an array of elements, so use [0] to get the first occurrence.

Example of How to JavaScript get element by name

If looking for a single element, take the first one from the nodelist, for example here is complete HTML code:

Output:

JavaScript get element by name

Do comment if you have any question or doubt or suggestions 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.