Event Delegation with jQuery Example using Do-It-Yourself

Thursday, March 4, 2010

Click on any of the rows in the table to toggle a highlight. This example is using the do-it-yourself method.

1 Lorem ipsum dolor sit amet
2 Lorem ipsum dolor sit amet
3 Lorem ipsum dolor sit amet
4 Lorem ipsum dolor sit amet
5 Lorem ipsum dolor sit amet
6 Lorem ipsum dolor sit amet

Example Code

jQuery(function($) {
    $('table').bind('click', function(event) {
        $(event.target).closest('tr').toggleClass('selected');
    });
});