0

$('input[type="checkbox"]').change(function() {
  var number = [];
  $('input[type="checkbox"]:checked').each(function() {
    number.push($(this).val());
  });
  if (number.length == 0) {
    $('.category1').prop('checked', true);
    loaddata();
  } else if ($('#all').checked && number.length != 8) {
    alert('it executed');
    $('#all').attr('checked', false);
    loaddata();
  } else {
    loaddata();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="expander-list" id="category">
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" id="all" class="category1">all</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="electronics" class="category1">Electronics</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="kitchen" class="category1">Kitchen</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="decoratives" class="category1">Decoratives / Interior</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="homedecor" class="category1">Home Decor</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="furnitures" class="category1">Furnitures</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="toys" class="category1">Toys</label>
    </div>
  </li>
  <li>
    <div class="radio" style="padding-left:0px">
      <label>
        <input type="checkbox" name="filter[]" value="vehicles" class="category1">Vehicles</label>
    </div>
  </li>
</ul>

My concern is very simple. by clicking on all jQuery is selecting all checkboxes but if one of them are unchecked than all checkbox are must be uncheck. but it not unchecking to all checkbox even my else if condition is not executing and it not alerting me. How to resolve it and thanks in advance.

2
  • 2
    $('#all').checked => $('#all').is(':checked') Commented Oct 13, 2016 at 10:16
  • 1
    Use prop() instead of attr() for "boolean" HTML attributes. Especially avoid a mix of both. Commented Oct 13, 2016 at 10:17

3 Answers 3

1

This is how I would do it:

$('input[type="checkbox"]').change(function() {
  var unchecked_flag = false;

  $('input[type="checkbox"]').slice(1).each(function() {
    if ($(this).prop("checked") == false) {
      unchecked_flag = true;
    }
  });

  if (unchecked_flag) {
    $("#all").prop("checked", false);
  } else {
    $("#all").prop("checked", true);
  }
});

$("#all").click(function() {
  $('input[type="checkbox"]').prop("checked", true);
});

Here is the JSFiddle demo

Sign up to request clarification or add additional context in comments.

Comments

1
  1. the statement $('#all').checked does not return what you expect.
    To get the checked property you need $('#all').prop('checked')

  2. Watch out the difference between .prop() and .attr().
    the first get the current checked property, the second will return the checked attribute (statically defined in the HTML page).
    Avoid using .attr() if you are looking for the current state of a checkbox.

Try with this snippet:

    $('input[type="checkbox"]').change(function() {
       var number = [];
       $('input[type="checkbox"]:checked').each(function() {
          number.push($(this).val());
       });
       if (number.length == 0) {
          $('.category1').prop('checked', true);
          loaddata();
       } else if ($('#all').prop('checked') && number.length !== 8) {
          alert('it executed');
          $('#all').prop('checked', false);
          loaddata();
       } else {
          loaddata();
       }
    });

See also jQuery docs

Comments

0

Hello According to your requirements I think the below fiddle will solve your problem try this hope it helps you.

I did it for both "all" and "single" checkbox for your help.you can choose the task whatever you wish..

**your jquery will be something like below..

<script>
$("#all").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));       
});
$(".category1").click(function() {
if( $(this).prop("checked") == false)
{
$(".category1").attr("checked", false);
$("#all").attr("checked", false);
};
});
</script>

Try the below Fiddle

Checkbox select Fiddle Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.