getElementsByName() property in HTML

getElementsByName() property access all elements which we will specify in it.
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function getNumberOfElements()
  5. {
  6. var x=document.getElementsByName("btn");
  7. alert(x.length);
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <input name="btn" type="button" value="First" />
  13. <input name="btn" type="button" value="Second" />
  14. <input name="btn1" type="button" value="Third" />
  15. <input name="btn" type="button" value="Forth" />
  16. <input name="btn" type="button" value="Fifth" />
  17. <input name="btn" type="button" value="Sixth" />
  18. <br />
  19. <br />
  20. <input type="button" onclick="getNumberOfElements()" value="Click here to find the number of elements that have the same name" />
  21. </body>
  22. </html>
In this example, the alert box shows the number of button elements (btn).
1.png