Categories
JavaScript Answers

How to get element by name with JavaScript?

Spread the love

To get element by name with JavaScript, we use the getElementsByName method.

For instance, we write

const val = document.getElementsByName("acc")[0].value;

to select the first element with name attribute acc with

document.getElementsByName("acc")[0]

Then we get its value with the value property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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