I have a list of check boxes on a page and disable/enable a button based on if one or more of the check boxes is checked. If something in the list is checked, the button gets enabled. Otherwise, it is disabled.
Since the list fairly long and it is also dynamic, this behavior is bound to the click event of each check box using live.
In jQuery 1.3.2 the following check worked as expected (with both actual and programmatic clicks) :
- if ($('input:checked').length > 0) {
- $('#button').removeAttr('disabled');
- } else {
- $('#button').attr('disabled', 'disabled');
- }
However, in jQuery 1.4.2 this behavior changes slightly. Actual clicks still register correctly and the code above functions as expected. Programmatic .click()s, on the other hand, pass through with the opposite behavior. When checking the state of $('input:checked').length afterward, it reports as I would have originally expected. I use this technique to unit test my code, so it is important that a programmatic click simulates an actual user click.
This appears to be related to the
known issue with the timing of :checked being switched when checking from a bound click handler. Although I didn't see this timing issue with the above code in the past, I have seen it when checking the state of a single check box when it was the one that was clicked. When checking a single check box, I could work around this by maintaining a state variable rather than checking the :checked state directly. Now that this same issue is showing up when checking multiple checkboxes (in addition to the one actually receiving the click) that work around becomes much more difficult.
Has anyone else experienced this or something similar? Is there a workaround that works well? Should this be filed as a bug even though the bug I linked to above is already logged?