'jQuery (UI): Detect checking of a checkbox

How can I make jQuery fire an event when a user checks a checkbox?

<input type="checkbox" id="test" name="test" /><label for="test">Check me</label>

I could do it with .click though that doesn't work when the user tabs to the checkbox. I wasn't able to find info on this in the API docs or while Googling.



Solution 1:[1]

$('#test').bind('change', function(){
    if($(this).is(':checked')){
        // box was checked
    }
});

Solution 2:[2]

You're looking for the change event:

$('#test').change(function() { ... });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jAndy
Solution 2 SLaks