Javascript DOM HTML document getElementsByName() Method get length

Introduction

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".

View in separate window

<!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>



PreviousNext

Related