peace_love
peace_love

Reputation: 6471

How can I check all checkboxes except the disabled ones?

If I click on checkAll I want to check all checkboxes except the disabled ones.

$("#checkAll").click(function () {
  $(".projectlist input:checkbox").each(function () { 
    if($(this).is(':enabled')){
     $('.projectlist input:checkbox').not(this).prop('checked', this.checked);
  }
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="projectlist">
<input id="checkAll" type="checkbox"><br>
<input disabled type="checkbox"><br>
<input disabled type="checkbox"><br>
<input type="checkbox"><br>
<input type="checkbox"><br>
</div>

Upvotes: 1

Views: 215

Answers (1)

guradio
guradio

Reputation: 15555

$("#checkAll").click(function() {

  $('.projectlist input:checkbox:enabled').prop('checked', this.checked);


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="projectlist">
  <input id="checkAll" type="checkbox"><br>
  <input disabled type="checkbox"><br>
  <input disabled type="checkbox"><br>
  <input type="checkbox"><br>
  <input type="checkbox"><br>
</div>

  1. use :enabled() selector

Description: Selects all elements that are enabled.

Upvotes: 4

Related Questions