Kyle Underhill
Kyle Underhill

Reputation: 109

add class to checked checkboxes using select all

I want to have the .active class added to every label of .select-input when .select-all is checked and then remove it when unchecked OR one of the .select-input is unchecked.

Why isn't adding .prop("checked", true) in the first function triggering the $("input[name='check']:checkbox").change(function() in the second?

$(document).ready(function() {
  $(".select-all").on("click", function() {
    $(this).is(":checked") ?
      $(".select-input").prop("checked", true) :
      $(".select-input").prop("checked", false);
  });
});
$("input[name='check']:checkbox").change(function() {
  if ($(this).is(":checked")) {
    $(this)
      .parent("label")
      .addClass("active");
  } else {
    $(this)
      .parent("label")
      .removeClass("active");
  }
});
.item {
  height: 50px;
  width: 50px;
  border: 3px solid;
  position: relative;
}

.pick-select {
  -webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

.pick-select.active {
  -webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -webkit-transform: scale(0.8);
  -ms-transform: scale(0.8);
  transform: scale(0.8);
  border: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="selectall">
<input type="checkbox" id="selectall" class="select-all"/> Select All
</label>
<div class="post-list">
  <div class="item">
    <div>
      <label class="pick-select" for="1"><input id="1" type="checkbox" class="select-input" name="check"></label> 1
    </div>
  </div>
  <div class="item">
    <div>
      <label class="pick-select" for="2"><input id="2" type="checkbox" class="select-input" name="check"></label> 2
    </div>
  </div>
</div>

Upvotes: 0

Views: 40

Answers (1)

Vineesh
Vineesh

Reputation: 3782

You need to use .change() after changing the property:

$(document).ready(function() {
  $(".select-all").on("click", function() {
    $(this).is(":checked") ?
      $(".select-input").prop("checked", true).change() :
      $(".select-input").prop("checked", false).change();
  });
});
$("input[name='check']:checkbox").change(function() {
  if ($(this).is(":checked")) {
    if($("input[name='check']:checkbox:not(:checked)").length==0){
         $(".select-all").prop("checked", true);
    }
    $(this)
      .parent("label")
      .addClass("active");
      
  } else {
    $(".select-all").prop("checked", false)
    $(this)
      .parent("label")
      .removeClass("active");
  }
});
.item {
  height: 50px;
  width: 50px;
  border: 3px solid;
  position: relative;
}

.pick-select {
  -webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

.pick-select.active {
  -webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
  -webkit-transform: scale(0.8);
  -ms-transform: scale(0.8);
  transform: scale(0.8);
  border: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="selectall">
<input type="checkbox" id="selectall" class="select-all"/> Select All
</label>
<div class="post-list">
  <div class="item">
    <div>
      <label class="pick-select" for="1"><input id="1" type="checkbox" class="select-input" name="check"></label> 1
    </div>
  </div>
  <div class="item">
    <div>
      <label class="pick-select" for="2"><input id="2" type="checkbox" class="select-input" name="check"></label> 2
    </div>
  </div>
</div>

Upvotes: 2

Related Questions