why is my checkbox.change not working (jquery)?
You shoud use this way :
$('body').on('change', 'input[type=checkbox]',function (e) {
//what ever you wanna do
});
(Because google search shows this page for another issues It make sense to explain more)
And If you want to check and uncheck programmatically with raising change event you should add .change() at the end of lines like these :
$('checkboxGroups').prop('checked', true).change();
$('checkboxGroups').prop('checked', false).change();
If the element has the 'change' event bound and if you need to fire the 'change' event after changing the checkbox property you can use the alt workaround below:
If you want to check:
$('#MyCheckbox input:checkbox').prop('checked', false).click();
If you want to uncheck:
$('#MyCheckbox input:checkbox').prop('checked', true).click();
Summary
it's using a reverse process. for the first one it will make sure the checkbox property is NOT checked, then it will trigger the click which will change the checkbox property to 'checked' then it will fire the 'change' event - if it's bound.
For dynamically created element you have to use event delegation
, if you want to select by an attribute you can use an attribute equals selector
.
Code:
$(document).on("change", "input[name='member']", function () {
alert("FECK");
if (this.checked) {}
});
Demo: http://jsfiddle.net/2fepaf0y/