If you attach to a change event, you can trigger the click event in jquery, which will check the checkbox and in turn fire the change event:
$(document).ready(function(){ $('#check_all').click(function(event) { $('[name^="checkbox"]').trigger("click"); }); $('[name^="checkbox"]').change(function(event) { console.log('a'); if ($(this).is(":checked")) { $(this).closest('tr').addClass('selected'); } else { $(this).closest('tr').removeClass('selected'); } }); });β
Updated script: http://jsfiddle.net/DC3EH/19/
The reason I switched to the change event instead of the click event is because jquery handles event triggering. When you start the click, it will call the event handler before it changes the state of the checkbox. Binding to a change ensures that your event is not triggered until the state changes.
source share