Main Tutorials

How to get element by name in HTML – getElementsByName

The getElementsByName() method is use to get the element by name. However be aware of the getElementsByName() method always return an array as output.










Caution
The method always return an array, and we have to use [] to access the value.

For example

alert(document.getElementsByName("myInput").value);

It will prompt out an undefined value

alert(document.getElementsByName("myInput")[0].value);

It will prompt out an expected ‘testing’ value

One more time, there are no getElementByName() exists, it is getElementsByName(), with a ‘s’ 🙂

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Anonymous
10 years ago

How you get all values? wiouth using for, I know with “getElementsByName” will give you all inputs, but I need each .value on every input, wiouth using a for later.
because the array is = []
you can use a for “array[i].value” to create another array is = [“some value”], but I dont want to use for =(

blm
9 years ago

getElementsByName doesn’t return an array, it returns a NodeList. While they look similar, they’re definitely not the same: https://developer.mozilla.org/en-US/docs/Web/API/NodeList#Why_is_NodeList_not_an_Array

So18oBVe75GIR 7zd5ILnb
4 years ago

GyIhidH7FpeJpwQt nx1S

Abed Kanbar
9 years ago

how I can use this method with JSF if I want to take the value of Please?

Peregrina
10 years ago

Explanation: This is because there can be multime inputs with the same name, such as checkboxes, radios and select options.

chaitanya
10 years ago

hi sir,
its working fine, but if i want to get the collection of all the names, then i was not able to get it.. plz help me out from this..

thanks in advance,
chaitanya

chaitanya
10 years ago

sir,
sometimes, its throwing me error that value of ‘c’ is undefined, when i use,

var c=0;

getElementsByName(‘name’)[c++].value;

this kind of error was thrown when i was trying to access my application on firefox…

[18:40:07.772] TypeError: document.getElementsByName(…)[(intermediate value)] is undefined

plz, sir, help me out…

thanks in advance.. 🙂

ertan ozgur
10 years ago

thank you Mkyong. ‘s’ letter and returning array gave me hard time

girish
11 years ago

Thank you so much it was useful to me alot…….

srinivas
11 years ago

thank u so much mkyong, this helped me a lot, intitially, i was using the property without “s”, n clearly emphasised on that,thanks a lot… 😉

hitesh
11 years ago

thanks this is help me

ketan
11 years ago
Reply to  hitesh

thanks

Luiz Antonio
11 years ago

Thanks a lot!!

Qwerty
12 years ago

But there is getElementById, which returns 1 element, because ID is supposed to be unique to each element. If not, the first occurence is returned.

getElementById

(without s, case-sensitive = not ID or id)

Alan
13 years ago

thanks a lot for this code ,it solved my problem

mxmz
14 years ago

Thanks, this helped alot