Javascript getElementsByName() not working - undefined value

How should I get the value using javascript getElementsByName() ? For some reason it is not working. I am getting only "undefined" value.
posted on 19.02.2021 at 18:19
0pnshow more
StorStor
Hi,

If value is assigned to element and you are getting undefined value, it means there is probably a problem with a javascript function. The function getElementsByName() returns a collection of all elements in the document with the specified name. Even when you use a unique specific name only for one element, you have to set the element's id in getElementsByName() function.

The using of getElementsByName() should look like:

<input name="zip_code" value="12345" />

<a href="javascript:void(0);" onClick="javascript: alert(document.getElementsByName('zip_code')[0].value); ">Get ZIP code</a>

posted on 21.02.2021 at 16:45
0pnshow more
TimTim
share on facebookshare on twitter
2021 AnswerTabsTermsContact us