Catch checked change event of a checkbox
<input type="checkbox" id="something" />
$("#something").click( function(){
if( $(this).is(':checked') ) alert("checked");
});
Edit: Doing this will not catch when the checkbox changes for other reasons than a click, like using the keyboard. To avoid this problem, listen to change
instead of click
.
For checking/unchecking programmatically, take a look at Why isn't my checkbox change event triggered?
The click will affect a label if we have one attached to the input checkbox?
I think that is better to use the .change() function
<input type="checkbox" id="something" />
$("#something").change( function(){
alert("state changed");
});
Use the :checked selector to determine the checkbox's state:
$('input[type=checkbox]').click(function() {
if($(this).is(':checked')) {
...
} else {
...
}
});