To act on a checkbox being checked or unchecked on click.
<input type="checkbox" id="customCheck1">
$('#customCheck1').click(function() {
if (this.checked) {
console.log('checked');
} else {
console.log('un-checked');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="customCheck1">
$('#customCheck1').click(function () {
if (this.checked == true) {
console.log('checked');
}
else {
console.log('un-checked');
}
});
EDIT: notNot a nice programming expression if (boolean == true)
though .checked
property might return other type variables as well..
It is better to use .prop("checked")
instead. It returns true
and false
only. Moreover, you avoid the bool == true
expression.