views:

31

answers:

3

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

A: 
$('#myform :checkbox').click(function() {
    var $this = $(this);
    // $this will contain a reference to the checkbox   
    if ($this.is(':checked')) {
        // the checkbox was checked 
    } else {
        // the checkbox was unchecked
    }
});
Darin Dimitrov
A: 
$('#myform input:checkbox').click(
 function(e){
   alert($(this).val())
 }
)
Thinker
+1  A: 

Use the change event.

$('#myform :checkbox').change(function() {
    // this represents the checkbox that was checked
    // do something with it
});
Anurag