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
0give a positive ratinggive a negative ratingshow more

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
0give a positive ratinggive a negative ratingshow more
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook
2022 AnswerTabsTermsContact us