Saturday, September 21, 2019

Javascript - document.getElementsByName() method

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

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


  1. document.getElementsByName("name")  
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.
  1. <script type="text/javascript">  
  2. function totalelements()  
  3. {  
  4. var allgenders=document.getElementsByName("gender");  
  5. alert("Total Genders:"+allgenders.length);  
  6. }  
  7. </script>  
  8. <form>  
  9. Male:<input type="radio" name="gender" value="male">  
  10. Female:<input type="radio" name="gender" value="female">  
  11.   
  12. <input type="button" onclick="totalelements()" value="Total Genders">  
  13. </form>  

Output of the above example

Male: Female: 

No comments:

Post a Comment