Kevin Kelly
Kevin Kelly

Reputation: 397

jQuery checkbox: select all/none except one

I have a checkbox select all issue. I have multiple checkbox that can be triggered by a master one.

If the master one is check then you can select any checkbox (which this works). Now my problem is when i check "none" all of them are gone even the master

What I need is not to unchecked the master. I can have as many as checkbox as I want.

Is there a solution to do this without putting an ID on each or automatically uncheck all checkbox and not the master one?

here is my code:

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">

  $(document).ready(function() {
  $('#checkAll').click(function() {
    if(!$('#master').is(':checked')) {  return;
    } $('input[type="checkbox"]').attr('checked', true);
  });

  $('#checkNone').click(function() {
    $('input[type="checkbox"]').attr('checked', false); });

  $('#master').click(function() { if($('#master').is(':checked')) {
        return; } $('input[type="checkbox"]').attr('checked', false);
  });
  $('input[type="checkbox"]').click(function() {
    if(!$('#master').is(':checked')) { $(this).attr('checked', false);
    }
  });
  });

  </script>
  </head>

  <input type="checkbox" value="master" id="master">master
  <span id="checkAll">All</span>
  <span id="checkNone">None</span>

  <input type="checkbox" value="1" id="c1">1
  <input type="checkbox" value="2" id="c2">2
  <input type="checkbox" value="3" id="c3">3
  <input type="checkbox" value="4" id="c4">4
  <input type="checkbox" value="5" id="c5">5

Upvotes: 15

Views: 4424

Answers (2)

Book Of Zeus
Book Of Zeus

Reputation: 49887

Based on your code, I would add a wrapper around the check-box you want to select all/none and then give the wrapper id and inputs to select all or none.

$('#list input[type="checkbox"]').attr('checked', false);

or for jQuery 1.6+

$('#list input[type="checkbox"]').prop('checked', false);

This way, you can control all your checkboxes without affecting the "master" one.

Here's the code:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$('#checkAll').click(function() {
  if(!$('#master').is(':checked')) {
      return;
  }
  $('#list input[type="checkbox"]').attr('checked', true);
});

$('#checkNone').click(function() {
  $('#list input[type="checkbox"]').attr('checked', false);
});

$('#master').click(function() {
  if($('#master').is(':checked')) {
      return;
  }
  $('#list input[type="checkbox"]').attr('checked', false);
});
$('#list input[type="checkbox"]').click(function() {
  if(!$('#master').is(':checked')) {
      $(this).attr('checked', false);
  }
});
});

</script>
</head>

<input type="checkbox" value="master" id="master">master
<span id="checkAll">All</span>
<span id="checkNone">None</span>

<div id="list">
 <input type="checkbox" value="1">1
 <input type="checkbox" value="2">2
 <input type="checkbox" value="3">3
 <input type="checkbox" value="4">4
 <input type="checkbox" value="5">5
</div>

Upvotes: 12

Tys
Tys

Reputation: 3610

You only need a very small modification to exclude your master.

You can do that with a .not("#master") like this:

  $('#checkNone').click(function() {
    $('input[type="checkbox"]').not("#master").attr('checked', false); });

Upvotes: 6

Related Questions