Strange question, I know. Allow me to explain.
I have a JavaScript/jQuery function that is used to handle checkbox changes. here’s some pseudo-code (FYI, I’m not actually trying to get the number of red rows – this is just PSEUDO CODE):
$("input[type='checkbox'].option").change(function() {
if ($(this).is(":checked")) {
$("div.special").removeClass("red");
}
else {
$("div.special").addClass("red");
}
// I only want to call this after all the checkbox event have been handled
updateNumRedDivs();
});
function updateNumRedDivs() {
alert($("div.special.red").length+"red DIVs found");
}
Now, the checkbox handler can be called either when an individual checkbox is clicked (obviously), or when a separate "Select All" checkbox is called:
$(".myForm .selectAllCheckbox").change(function() {
var $checkboxes = $(this).closest("form").find("input[type='checkbox'].option");
if ($(this).attr("checked")) {
$checkboxes.each(function() {
if (!$(this).attr("checked"))
$(this).attr("checked", "checked").change();
});
}
updateNumRedDivs();
});
I hope this makes sense.
here’s the solution I came up with – not pretty, but it gets the job done:
Basically, I consolidated into one change handler, and made it recursive.