I'm new to jQuery
and I need to check if the checkbox is checked. In other posts I saw that I need to use .is(":checked")
to solve it, but somehow it doesn't work.
$('.neutral').on('click', function() {
var checkbox = $(this);
if (checkbox.is(":checked")) {
console.log('checked');
} else {
console.log('unchecked');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="neutral" />
In this code I have 2 problems and I don't know how to solve it.
When I'm using
console.log('checked')
outside of the if statement (aftercheckbox
variable) and I click on the checkbox one time, console prints the result 2 times.I don't know why this if statement doesn't working.
Thank you for your time and help.