The getElementsByName()
method accesses all
elements with the specified name.
document.getElementsByName(name)
Parameter | Description |
---|---|
name | Required. The name of the element. |
getElementsByName |
Yes | Yes | Yes | Yes | Yes |
The following code shows how to get the number of elements with a specific name.
<!DOCTYPE html>
<html>
<head>
<script>
function getElements()<!-- w w w .jav a 2 s . com-->
{
var x=document.getElementsByName("x");
console.log(x.length);
}
</script>
</head>
<body>
A<input name="x" type="radio" value="A">
B<input name="x" type="radio" value="B">
<input type="button" onclick="getElements()" value="How many elements named 'x'?">
</body>
</html>
The code above is rendered as follows: