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.
19 Feb 2021 at 06:19 PM
0pnshow more
StorStor
Find us on Facebook AnswertabsFind us on Facebook Answertabs
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>

21 Feb 2021 at 04:45 PM
0pnshow more
TimTim
share on facebookshare on twitter
2021 AnswerTabsTermsContact us