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.
give a positive ratinggive a negative rating
19 Feb 2021 at 06:19 PM

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>

give a positive ratinggive a negative rating
21 Feb 2021 at 04:45 PM
Share on FacebookShare on TwitterShare on LinkedInSend email
2023 AnswerTabsTermsContact us
How would you rate your experience with AnswerTabs ?
Very badVery good
Thank you for your feedback.