Group checkboxes check/uncheck

Group checkboxes check/uncheck

I am working on group of checkboxes for check/uncheck. I did solve for that, but I having another issue outside the group of checkboxes.

I kept a checkbox out side group of checkboxes called chkSolicitation and when check this all group of checkboxes  should be unchecked. 

If any of the group of checkboxes checked then automatically  chkSolicitation should be unchecked.   Onload also If  group of checkboxes unchecked  automatically  chkSolicitation  should be  unchecked and Vice versa.


  1. <fieldset>
  2.     <!-- these will be affected by check all -->
  3.     <div><input type="checkbox" ID="checkall1"/> Check all</div>
  4.     <div><input type="checkbox"/> Checkbox</div>
  5.     <div><input type="checkbox"/> Checkbox</div>
  6.     <div><input type="checkbox"/> Checkbox</div>
  7. </fieldset>
  8. <fieldset>
  9.     <!-- these won't be affected by check all; different field set -->
  10.     <div><input type="checkbox" ID="checkall2"/> Check all</div>
  11.     <div><input type="checkbox"/> Checkbox</div>
  12.     <div><input type="checkbox"/> Checkbox</div>
  13.     <div><input type="checkbox"/> Checkbox</div>
  14. </fieldset>
  15. <br/>
  16.  <div><input type="checkbox" ID="chkSolicitation"/> Please do not send me any email</div>
Javascript
  1. $('[id^=checkall]').click(function(){
  2.     $(this).closest('fieldset').find('input').not(this).prop('checked',this.checked);
  3. });

  4. $(':checkbox').not('[id^=checkall]').click(function(){
  5.     var all = $(this).closest('fieldset').find('[id^=checkall]');
  6.     var chks = $(this).closest('fieldset').find('input').not(all);
  7.     
  8.     all.prop('checked', chks.length == chks.filter(':checked').length);
  9. })