I have a class that adds a fake checkbox and using jQuery, once the user clicks it, add the checked state class to the fake checkbox.
CSS
.fake-checkbox { /* ... */ }
.fake-checkbox.checked-state { /* ... */ }
HTML
<label>
<input type="checkbox">
<div class="fake-checkbox"></div>
</label>
JS
(function($) {
$('.fake-checkbox').click(function() {
// Check if fake-checkbox has class checked-state, then remove the class checked-state and vice versa.
if ($(this).hasClass('checked-state')) {
$(this).removeClass('checked-state');
} else {
$(this).addClass('checked-state');
}
});
}(jQuery));
Now, I also want to make the input checkbox in its checked state at the same time when the class is added and in its unchecked state when the class is removed.
I know this can be done with element.checked = true
but not in jQuery.
How can I achive this?
EDIT
This is surely different and not a duplicate of this question cause we're in a different case, although there's a similarity about 'ticking a checkbox using jQuery' but still not a possible duplicate.
Thanks.