Sowemimo Bamidele
Sowemimo Bamidele

Reputation: 129

How can i correct this jquery / js checkbox code

I've been having a hard time figuring this codes out.

The checkbox codes functions like this:

There is a global checkbox (Check all) and there are child (Single) checkboxes. If the (global) checkbox is checked, all the (child) checkboxes will be checked as well, a div will show, and if the global is unchecked, it un-checks the (child) checkboxes and the div will hide (jquery hide and show). The the numbers of the checked checkboxes will be displayed.

This is the problem; is if a child checkbox is unchecked, the global checkbox still remains checked and if all the child checkboxes are checked, the global checkbox should be checked immidiately as well.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
 <div id="mydiv" style="display:none;">RESTORE | DELETE
 <span>Checked: </span>
 <span id="counter"></span>
 </div>
<input type="checkbox" id="global">
<br>
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">

    $(document).ready(function() {
  $('#global').click(function() {
    $('.child').prop('checked', $(this).is(':checked'));

    if ($(this).is(':checked'))
      $('#mydiv').show();
    else
      $('#mydiv').hide();

    count();
  });

  $('.child').change(function() {
    var checkedLength = $('.child:checked').length;
    if (checkedLength > 0)
      $('#mydiv').show();
    else
      $('#mydiv').hide();
    count();
  });
});

var count = function() {
   var i = $('input.child:checked').length;
   $('#counter').html(i);
}

All support are apreciated. Thanks in advance.

Upvotes: 0

Views: 139

Answers (3)

PeterKA
PeterKA

Reputation: 24648

$(document).ready(function() {
  var div = $('#mydiv'),
      global = $('#global'),
      childchecks = $(':checkbox.child');

  global.on('change', function() {
    childchecks.prop('checked', this.checked);
    var how = this.checked ? 'show' : 'hide';
    div[how]();
  });

  childchecks.on('change', function() {
    global.prop('checked', childchecks.length === childchecks.filter(':checked').length );
    var how = childchecks.length === childchecks.filter(':checked').length ? 'show' : 'hide';
    div[how]();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
 <div id="mydiv" style="display:none;">RESTORE | DELETE
 <span>Checked: </span>
 <span id="counter"></span>
 </div>
<input type="checkbox" id="global">
<br>
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">

Upvotes: 1

DannielR
DannielR

Reputation: 148

change #global to .global

This should work. Only one event is needed.

$(document).ready(function() {
  var child = $('.child');
  var global = $('.global');

  $('input').on("change", function() {
    //check if checkbox is the global one
    if($(this).hasClass("global")) {
      //if yes, then set all childs to checked true, if not, then to false
      ($(this).prop('checked')) ? child.prop('checked', true) : child.prop('checked', false);
    } else {
      var oneChecked = false;
      //every change on a checkbox go though all childboxes an check if on of them is checked
      child.each(function() {
        if($(this).prop('checked') == true) {
          oneChecked = true;
        }
      });
      //if one was checked global has to be checked, if no one was checked it has to be false
      (oneChecked) ? global.prop('checked', true) : global.prop('checked', false);
    } 
  });
});

Upvotes: 2

Rapha&#235;l Pellicier
Rapha&#235;l Pellicier

Reputation: 192

Try :

$(document).ready( function(){
    $('#global').change( function(){
        var bIsChecked = $(this).is(':checked');
        $('.child').prop('checked', bIsChecked );
        $('#mydiv')[ bIsChecked ? 'show' : 'hide' ]();
        count();
    });

    $('.child').change( function(){
        var n = count();
        $('#mydiv')[ n>0 ? 'show' : 'hide' ]();
        $('#global')[0].checked = n==$('.child').length ;
    });
});

var count =function(){
   var n = $('.child:checked').length;
   $('#counter').html( n );
   return n
}

Upvotes: 0

Related Questions