Document getElementsByName() Method - Check all <input> elements with type="checkbox" with name attribute value "animal": - Javascript DOM

Javascript examples for DOM:Document getElementsByName

Description

Document getElementsByName() Method - Check all <input> elements with type="checkbox" with name attribute value "animal":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>
Cats:<input name="animal" type="checkbox" value="Cats">
Dogs:  <input name="animal" type="checkbox" value="Dogs">

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  w w w.j a  va  2 s  . c  o  m
    var x = document.getElementsByName("animal");
    var i;
    for (i = 0; i < x.length; i++) {
        if (x[i].type == "checkbox") {
            x[i].checked = true;
        }
    }
}
</script>

</body>
</html>

Related Tutorials