So I have a business case where I've got groups (called Bundles) and they can contain other Bundles. Now, in my interface, I'm trying to have it so when you check the top level, it automatically checks (and disables, but I'm not there yet) the child bundles.
In order to accomplish that, each checkbox has an onchange event where it passes in this
and a comma-delimited list of other Bundles that should be checked. The code I've pasted below behaves unexpectedly. Namely, when I'm forcing the Change event to be called (because programatically manipulating the checked state doesn't raise the Change event) the subsequent call winds up using the exact same arguments that the original Change event used.
It's almost as if when I call .change()
, it's passing my original checkbox (not the cascaded one) and original list of children.
I've put in a ton of alerts, and the clutch one says "about to trigger change for [the right checkbox]", but then the very next alert says "disable raised for [the original/wrong checkbox]".
Any ideas on why programmatically raising the Change event is causing its arguments to be all messed up?
function disableChildren(chkBundle, childBundles) {
var bundleId = chkBundle.id.substr(chkBundle.id.lastIndexOf("chk"));
alert("disable raised for '" + bundleId + "' using children '" + childBundles + "'");
jQuery("#BundleList input:checkbox[id*=" + bundleId + "id]").attr("checked", chkBundle.checked);
var childIds = childBundles.split(",");
for (var i = 0; i < childIds.length; i++) {
jQuery("#BundleList input:checkbox[id$=chk" + childIds[i] + "]").each(function(index, domEle) {
if (domEle.checked != chkBundle.checked) {
alert('about to check ' + domEle.id);
domEle.checked = chkBundle.checked;
alert('about to trigger change for ' + domEle.id);
domEle.change();
alert('done triggering ' + domEle.id);
}
});
}
}
And an example of one of the html for a checkbox:
<input id="BundleAssignment_rptMainBundle_ctl02_chk1"
type="checkbox" name="BundleAssignment$rptMainBundle$ctl02$chk1"
onchange="disableChildren(this,'7,8')" onclick="disableChildren(this,'7,8')" />