I have recently discovered an issue that I can't seem to figure out regarding jQuery and checkbox event handling.
What should happen is:
- Click a box-image thats associated with a specific checkbox.
- When the box-image is marked as checked (denoted by adding a class of .checked and displaying top right corner checkmark indicator) the associated checkbox should also be checked.
When I repeatedly/quickly click on the gray box-image tiles you'll see the bug in question. The default behavior (toggling the checked attribute) appears to randomly take place after the event handlers fire. As a result this cause the associated selected box-image and checkbox to randomly unsync with one another, thereby giving inaccurate data.
Please view this example and bug in action by going to https://jsfiddle.net/coreyds/5wLk2rge/4/
Do you have a solution to fix this problem either in jQuery or plain JavaScript?
Here is the code in question:
HTML:
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="form-group hwl-inline-checkbox-group">
<label class="checkbox-inline">
<div id="sa-checkbox-img1" class="sa-checkbox-img">
<i class="fa fa-check"></i>
<i class="fa fa-briefcase fa-3x"></i>
<label class="icon-img-label">WORK</label>
</div>
<input type="checkbox" id="imgCheck1" name="surrounding_area" class="sa-checkbox" value="work" />
</label>
<label class="checkbox-inline">
<div id="sa-checkbox-img2" class="sa-checkbox-img">
<i class="fa fa-check"></i>
<i class="fa fa-building fa-3x"></i>
<label class="icon-img-label">SCHOOL</label>
</div>
<input type="checkbox" id="imgCheck2" name="surrounding_area" class="sa-checkbox" value="school" />
</label>
<label class="checkbox-inline">
<div id="sa-checkbox-img3" class="sa-checkbox-img">
<i class="fa fa-check"></i>
<i class="fa fa-meh-o fa-3x"></i>
<label class="icon-img-label">NO PREFERENCE</label>
</div>
<input type="checkbox" id="imgCheck3" name="surrounding_area" class="sa-checkbox" value="no preference" />
</label>
</div>
</div>
</div>
<!-- end .row -->
jQuery
$('.sa-checkbox-img').click(function(){
var $this = $(this),
sa_checkbox = $this.find($('.sa-checkbox'));
if( !$this.hasClass('checked')){
$this.addClass('checked');
sa_checkbox.prop('checked', true);
} else {
$this.removeClass('checked');
sa_checkbox.prop('checked', false);
}
});
CSS
.container,
.row {
margin-top: 20px;
}
h5 {
text-align: center;
}
.sa-checkbox-img {
color: #999;
border: 5px solid #999;
padding: 10px 25px;
text-align: center;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-moz-transition-duration: 250ms;
-o-transition-duration: 250ms;
-webkit-transition-duration: 250ms;
transition-duration: 250ms;
-moz-transition-property: all;
-o-transition-property: all;
-webkit-transition-property: all;
transition-property: all;
}
.sa-checkbox-img label.icon-img-label {
display: block;
}
.sa-checkbox-img i.fa.fa-check {
display: none;
color: white;
}
.sa-checkbox-img:hover {
border-color: #009fd4;
color: #009fd4;
cursor: default;
}
.sa-checkbox-img.checked {
border-color: #009fd4;
color: #009fd4;
cursor: default;
}
.sa-checkbox-img.checked i.fa.fa-check {
display: block;
background: #009fd4;
position: absolute;
top: -5px;
right: -5px;
color: white;
padding: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}