How to get element by name in HTML – getElementsByName
Written on April 24, 2009 at 11:53 am by
mkyong
The getElementsByName() method is use to get the element by name. However be aware of the getElementsByName() method always return an array as output.
<html>
<head>
<script type="text/javascript">
function showElements(){
alert(document.getElementsByName("myInput")[0].value);
}
</script>
</head>
<body>
<form >
<input name="myInput" type="text" value='testing'><br>
<br>
<input name="submit" type="button" onclick="showElements()"
value="Show Me Output">
</form>
</body>
</html>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’
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation



thanks a lot for this code ,it solved my problem
Thanks, this helped alot
wc, good to know this is helpful.