Javascript Reference - HTML DOM getElementsByName() Method








The getElementsByName() method accesses all elements with the specified name.

Syntax

document.getElementsByName(name)

Parameter

Parameter Description
name Required. The name of the element.

Browser Support

getElementsByName Yes Yes Yes Yes Yes




Example

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: