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 the Author

author image
mkyong
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

avatar
15 Comment threads
2 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
16 Comment authors
So18oBVe75GIR 7zd5ILnbblmAbed KanbarPeregrinachaitanya Recent comment authors
newest oldest most voted
Anonymous
Guest
Anonymous

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
Guest
blm

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
Guest
So18oBVe75GIR 7zd5ILnb

GyIhidH7FpeJpwQt nx1S

Abed Kanbar
Guest
Abed Kanbar

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

Peregrina
Guest
Peregrina

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

chaitanya
Guest
chaitanya

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
Guest
chaitanya

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
Guest
ertan ozgur

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

girish
Guest
girish

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

srinivas
Guest
srinivas

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
Guest
hitesh

thanks this is help me

ketan
Guest
ketan

thanks

Luiz Antonio
Guest
Luiz Antonio

Thanks a lot!!

Qwerty
Guest
Qwerty

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
Guest
Alan

thanks a lot for this code ,it solved my problem

mxmz
Guest
mxmz

Thanks, this helped alot