I have the following:
<form id="myform"> <input type="checkbox" name="check1" value="check1"> <input type="checkbox" name="check2" value="check2"> </form>
How do I use jQuery to capture any check event occuring in myform
and tell which checkbox was toggled (and know if it was toggled on or off)?
change() updates the textbox value with the checkbox status. I use . click() to confirm the action on uncheck. If the user selects cancel, the checkmark is restored but .
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.
Checkbox event handling using pure Javascript There are two events that you can attach to the checkbox to be fired once the checkbox value has been changed they are the onclick and the onchange events.
Use the change() event, and the is() test: $('input:checkbox'). change( function(){ if ($(this).is(':checked')) { alert('checked'); } });
$('#myform :checkbox').change(function() { // this will contain a reference to the checkbox if (this.checked) { // the checkbox is now checked } else { // the checkbox is now no longer 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