Get elements by name in javascript
Getting elements by name attribute can done using getElementsByName
method of document. This method takes value of name attribute as argument.
It returns array of elements because more than one element(s) can have same name.
It returns array of elements because more than one element(s) can have same name.
<input type="button" value="Get By Name" onclick="GetByName()" />
<input type="text" name="txtNumber" />
<input type="text" name="txtNumber" />
<input type="text" name="txtNumber" />
<script type="text/javascript">
function GetByName() {
var elements = document.getElementsByName("txtNumber");
if (elements.length > 0) {
alert(elements[0].value);
}
else
alert("No matching element found");
}
</script>
Comments
Post a Comment