jQuery checkbox event handling

aeq picture aeq · Aug 9, 2010 · Viewed 277.5k times · Source

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)?

Answer

Darin Dimitrov picture Darin Dimitrov · Aug 9, 2010
$('#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
    }
});