Using the new 'on' method in jQuery (1.7): http://api.jquery.com/on/
$('#myform').on('change', 'input[type=checkbox]', function(e) {
console.log(this.name+' '+this.value+' '+this.checked);
});
$('#myform input:checkbox').click(
function(e){
alert($(this).is(':checked'))
}
)
I have try the code from first answer, it not working but I have play around and this work for me
$('#vip').change(function(){
if ($(this).is(':checked')) {
alert('checked');
} else {
alert('uncheck');
}
});
Use the change event.
$('#myform :checkbox').change(function() {
// this represents the checkbox that was checked
// do something with it
});
There are several useful answers, but none seem to cover all the latest options. To that end all my examples also cater for the presence of matching label elements and also allow you to dynamically add checkboxes and see the results in a side-panel (by redirecting console.log).
Listening for click events on checkboxes is not a good idea as that will not allow for keyboard toggling or for changes made where a matching label element was clicked. Always listen for the change event.
Use the jQuery :checkbox pseudo-selector, rather than input[type=checkbox]. :checkbox is shorter and more readable.
Use is() with the jQuery :checked pseudo-selector to test for whether a checkbox is checked. This is guaranteed to work across all browsers.
$('#myform :checkbox').change(function () {
if ($(this).is(':checked')) {
console.log($(this).val() + ' is now checked');
} else {
console.log($(this).val() + ' is now unchecked');
}
});
JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/2/
:checkbox selector, which is preferable to using input[type=checkbox]Delegated event handlers are designed for situations where the elements may not yet exist (dynamically loaded or created) and is very useful. They delegate responsibility to an ancestor element (hence the term).
$('#myform').on('change', ':checkbox', function () {
if ($(this).is(':checked')) {
console.log($(this).val() + ' is now checked');
} else {
console.log($(this).val() + ' is now unchecked');
}
});
JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/4/
change) to bubble up to a non-changing ancestor element (in this case #myform). ':checkbox' in this case) to only the elements in the bubble chain.document as the default to connect the delegated event handler, if nothing else is closer/convenient. body to attach delegated events as it has a bug (to do with styling) that can stop it getting mouse events.The upshot of delegated handlers is that the matching elements only need to exist at event time and not when the event handler was registered. This allows for dynamically added content to generate the events.
Q: Is it slower?
A: So long as the events are at user-interaction speeds, you do not need to worry about the negligible difference in speed between a delegated event handler and a directly connected handler. The benefits of delegation far outweigh any minor downside. Delegated event handlers are actually faster to register as they typically connect to a single matching element.
prop('checked', true) fire the change event?This is actually by design. If it did fire the event you would easily get into a situation of endless updates. Instead, after changing the checked property, send a change event to the same element using trigger (not triggerHandler):
e.g. without trigger no event occurs
$cb.prop('checked', !$cb.prop('checked'));
JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/5/
e.g. with trigger the normal change event is caught
$cb.prop('checked', !$cb.prop('checked')).trigger('change');
JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/6/
Notes:
triggerHandler as was suggested by one user, as it will not bubble events to a delegated event handler.JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/8/
although it will work for an event handler directly connected to the element:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/u8bcggfL/9/
Events triggered with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.
Reference: http://api.jquery.com/triggerhandler/
If anyone has additional features they feel are not covered by this, please do suggest additions.
Acknowledging the fact that the asker specifically requested jQuery and that the answer selected is correct, it should be noted that this problem doesn't actually need jQuery per say. If one desires to solve this problem without it, one can simply set the onClick attribute of the checkboxes that he or she wants to add additional functionality to, like so:
HTML:
<form id="myform">
<input type="checkbox" name="check1" value="check1" onClick="cbChanged(this);">
<input type="checkbox" name="check2" value="check2" onClick="cbChanged(this);">
</form>
javascript:
function cbChanged(checkboxElem) {
if (checkboxElem.checked) {
// Do something special
} else {
// Do something else
}
}
Fiddle: http://jsfiddle.net/Y9f66/1/