Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Change Event on working in jQuery

When I click on the single checkbox, it changes and green colored. But when I check Full day, all checkboxes are checked but color not change. also after checking full Day, I uncheck all times still full day is checked. I'm stuck, what wrong with this code?

$(document).ready(function() {


  $('input:checkbox[name="time"]').change(function() {
    $('input:checkbox[name="time"]:not(:checked)').parent().removeClass("active");
    $('input:checkbox[name="time"]:checked').parent().addClass("active");
  });
});

function selectAll(source) {
  checkboxes = document.getElementsByName('time');
  for (var i in checkboxes)
    checkboxes[i].checked = source.checked;
}
.timing {
  width: 500px;
}

.timing label {
  width: 100px;
  display: inline-block;
  border: 1px solid #ccc;
  padding: 10px;
  text-align: center;
  cursor: pointer;
}

.timing label input {
  display: block;
}

.timing label.active {
  background-color: rgba(0, 204, 0, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timing">
  <label for="11:30"><input name="time" class="timess" value="11:30" id="11:30" type="checkbox">11:30</label>
  <label for="12:00"><input name="time" class="timess" value="12:00" id="12:00" type="checkbox">12:00</label>
  <label for="12:30" class=""><input name="time" class="timess" value="12:30" id="12:30" type="checkbox">12:30</label>
</div>



<label for="selectall"><input type="checkbox" id="selectall" onClick="selectAll(this)" />Full Day</label>

<script>
  function selectAll(source) {
    checkboxes = document.getElementsByName('time');
    for (var i in checkboxes)
      checkboxes[i].checked = source.checked;
  }
</script>
like image 937
Champ Decay Avatar asked Jul 14 '17 07:07

Champ Decay


2 Answers

The issue is because you need to trigger a change event on the checkboxes when clicking the 'Select All' option so that their own event handler runs and changes their background colour. This does not occur when setting the checked state manually, so you can use the trigger() method in your code.

You should note though, that you can improve your logic by using toggleClass() and also removing the on* event attribute as they are outdated. Use an unobtrusive event handler as you do for the normal checkboxes. Try this:

$('input:checkbox[name="time"]').change(function() {
  $(this).closest('label').toggleClass('active', this.checked);
});

$('#selectall').change(function() {
  $('input:checkbox[name="time"]').prop('checked', this.checked).trigger('change');
});
.timing {
  width: 500px;
}

.timing label {
  width: 100px;
  display: inline-block;
  border: 1px solid #ccc;
  padding: 10px;
  text-align: center;
  cursor: pointer;
}

.timing label input {
  display: block;
}

.timing label.active {
  background-color: rgba(0, 204, 0, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timing">
  <label for="11:30">
    <input name="time" class="timess" value="11:30" id="11:30" type="checkbox">
    11:30
  </label>
  <label for="12:00">
    <input name="time" class="timess" value="12:00" id="12:00" type="checkbox">
    12:00
  </label>
  <label for="12:30">
    <input name="time" class="timess" value="12:30" id="12:30" type="checkbox">
    12:30
  </label>
</div>

<label for="selectall">
  <input type="checkbox" id="selectall" />
  Full Day
</label>
like image 165
Rory McCrossan Avatar answered Oct 15 '22 17:10

Rory McCrossan


I added trigger event. Change event will not get fired if you check checkbox using JS.

function selectAll(source) {
    checkboxes = document.getElementsByName('time');
    for(var i in checkboxes)
        checkboxes[i].checked = source.checked;
        $('input:checkbox[name="time"]').trigger('change');//Trigger change event to checkbox
}

$(document).ready(function () {
	
 
	$('input:checkbox[name="time"]').change(function () {
        $('input:checkbox[name="time"]:not(:checked)').parent().removeClass("active");
        $('input:checkbox[name="time"]:checked').parent().addClass("active");
    });    
});

function selectAll(source) {
	checkboxes = document.getElementsByName('time');
	for(var i in checkboxes)
		checkboxes[i].checked = source.checked;
        $('input:checkbox[name="time"]').trigger('change')
}
.timing{width:500px;}
.timing label{width:100px;display:inline-block;border:1px solid #ccc;padding:10px;text-align:center;cursor:pointer;}
.timing label input{display:block;}
.timing label.active{background-color:rgba(0,204,0,1);}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timing">
<label for="11:30"><input name="time" class="timess" value="11:30" id="11:30" type="checkbox">11:30</label>
<label for="12:00"><input name="time" class="timess" value="12:00" id="12:00" type="checkbox">12:00</label>
<label for="12:30" class=""><input name="time" class="timess" value="12:30" id="12:30" type="checkbox">12:30</label>
</div>



<label for="selectall"><input type="checkbox" id="selectall" onClick="selectAll(this)" />Full Day</label>

<script>
function selectAll(source) {
	checkboxes = document.getElementsByName('time');
	for(var i in checkboxes)
		checkboxes[i].checked = source.checked;
}
</script>
like image 29
Ved Avatar answered Oct 15 '22 16:10

Ved