I want to check if a checkbox is checked. I tried the following 4 methods in jQuery, but none is working:
$('#first').change(function() {
if ($(this).is(':checked')) {
console.log('checked1');
// perform action
}
if ($('#first:checked').length) {
// perform action
console.log('checked2');
}
if ($('#first').is(':checked')) {
// perform action
console.log('checked3');
}
if ($('#first').attr('checked')) {
// perform action
console.log('checked4');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="" id="first">