I'm trying to see if a checkbox is checked or not with a simple function. It doesn't seem to be working, here's the code:
HTML:
<form>
<input type="checkbox" id="check" />
</form>
JS:
$(document).ready(function() {
if ($('#check').is(":checked")) {
alert('it works')
}
});
JSFiddle: https://jsfiddle.net/5h58mx1h/
Your code only runs once - when document
is ready. You need to attach an event listener to the checkbox and check when it changes:
$(document).ready(function() {
$('#check').change(function() {
if ($(this).is(":checked")) {
alert('it works');
}
});
});
Fiddle
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