Skip to content Skip to sidebar Skip to footer

Check And Un-check Checkboxes Using Javascript

Hey guys im trying to get a checkbox scenario worked out, I have 7 boxes and im trying to get a logic statement where it works things out. I have 7 checkboxes and the 7th box is al

Solution 1:

I first suggest that you give a specific NAME attribute to the 1-6 checkboxes, and parsing them using getElementsByName like so :

<inputtype="checkbox"id="myChk1"name="myChk" />
...
<inputtype="checkbox"id="myChk6"name="myChk" /><inputtype="checkbox"id="myChkAll"onchange="chkAll(this);" /><scripttype="text/javascript">functionchkAll(obj) {
    var isChecked = obj.checked;

    var chk1to6 = document.getElementsByName('myChk');

    for (var i = 0 ; i < chk1to6.length ; i++) {
        chk1to6[i].checked = isChecked;
    }
}
</script>

Solution 2:

Give different unique Id to all the checkboxs... like

  • chckbx1

  • chckbx2

  • chckbx3

    .

    .

  • chckbx7

the call a same function on click of any of the checkbox with the object of that checkbox

i.e. onclick=functionname(this);

In side the function check the id

functioname(str){
if(str.id=="chckbx7"){
//deselect allexcept chckbx7
}
else{
//deselect chckbx7
}
}

Post a Comment for "Check And Un-check Checkboxes Using Javascript"