Learn JavaScript for Web Development

JavaScript is most versatile languages to code applications for the browser, desktop, phone or tablet. Write program for client browser or web server. Build interactive web site with JavaScript.

document.getElementByName Method

The document.getElementsByName() method returns all the element of specified name.

The syntax of the getElementsByName() method is given below:


Here, name is required.

Example of document.getElementsByName() method

In this example, we going to count total number of genders. Here, we are using getElementsByName() method to get all the genders.

<script type="text/javascript">
function totalelements()
var allgenders=document.getElementsByName("gender");
alert("Total Genders:"+allgenders.length);
Male:<input type="radio" name="gender" value="male">
Female:<input type="radio" name="gender" value="female">

<input type="button" onclick="totalelements()" value="Total Genders">


Male: Female: