mercredi 7 octobre 2015

How to remove checked checkbox if all sub check boxes are unchecked?

I have group of checkboxes (Sub1,Sub2,Sub3) and One main check box. If check any sub check box the main checkbox have to be checked. If all the sub check boxes are uncheck then only the main check box have to be unchecked,if atleast one checkbox is checked then the main checkbox have to be checked.My code is given below.

<!DOCTYPE html>
<html>
  <script type='text/javascript' src="jquery-1.10.1.js"></script>
<script type='text/javascript'>

$(window).load(function(){
    var chk1 = $("input[type='checkbox'][value='1']");

    var chk2 = $("input[type='checkbox'][value='2']");
    var chk3 = $("input[type='checkbox'][value='3']");
    var chk4 = $("input[type='checkbox'][value='4']");

    chk2.on('change', function(){

                chk1.prop('checked',this.checked);

    });

    chk3.on('change', function(){

                chk1.prop('checked',this.checked);

    });
    chk4.on('change', function(){

                chk1.prop('checked',this.checked);
     });

}); 

</script>

</head>

<body>

<input type="checkbox" value="1" class="user_name">Main1<br><br><br>

<input type="checkbox" value="2" id ="one" class="user_name">sub-2<br>
<input type="checkbox" value="3" id ="one" class="user_name">sub-3<br>
<input type="checkbox" value="4" id ="one" class="user_name">sub-4<br>




Aucun commentaire:

Enregistrer un commentaire