0

I have tried the below code to uncheck the 1st checkbox if any one of the below are checked. But not able to revert back to the initial stage i.e. if nothing is selected the first checkbox will checked.

$('ol input').on('click', function() {
  if(this.checked){  
    $('#all').prop('checked', false);
  }else{
    $('#all').prop('checked', true);
  }
});
ol li{list-style:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <input type="checkbox" id="all" checked>All
  <ol>
    <li>
      <input type="checkbox">1
      <input type="checkbox">2
      <input type="checkbox">3
      <input type="checkbox">4
      <input type="checkbox">5
    <li>
  </ol>
</div>

1 Answer 1

1

Instead of your current else statement, try else if ($('ol input:checked').length == 0)

$('ol input').on('click', function() {
  if (this.checked) {
    $('#all').prop('checked', false);
  } else if ($('ol input:checked').length == 0) {
    $('#all').prop('checked', true);
  }
});
ol li {
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <input type="checkbox" id="all" checked>All
  <ol>
    <li>
      <input type="checkbox">1
      <input type="checkbox">2
      <input type="checkbox">3
      <input type="checkbox">4
      <input type="checkbox">5
      <li>
  </ol>
</div>

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

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.