Find out how many elements that have a name attribute with the value "animal":
var x = document.getElementsByName("animal").length;
Click the button to find out how many elements there are in the document that have a name attribute with the value "animal".
<!DOCTYPE html> <html> <body> Cats://ww w . jav a 2 s . co m <input name="animal" type="checkbox" value="Cats"> Dogs: <input name="animal" type="checkbox" value="Dogs"> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementsByName("animal").length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>