Hi after a lot of search I got a method to check my checkbox. But I dont know what is the problem. Its val() always return on. This is happening when I check it and also when I uncheck it. I suppose it should return null when unchecked. Help
$(document).ready(function(){
$('#ch').change(function(){
alert($(this).val());
});
});
and the html
<input type="checkbox" id="ch"/> Check it out...
checked always returns false, the html is correct. The HTML may be correct but you need to add it to your question.
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
You have set of option to check if your checkbox
is checked or unchecked,
alert($(this).is(":checked")); ////return true if checked
Or
alert(this.checked); //return true if checked
Or
<input type="checkbox" id="ch" checked/>
alert($(#ch).attr( "checked")); //return checked if checked
Or
alert($(this).prop( "checked")); //return true if checked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With