jQuery event delegation

/ Published in: jQuery
Save to your folder(s)

Note that this is more efficient than binding an event to every individual TD. Plus, as a bonus, any new TD's added are bound with this method. When using event delegation you can add as many matching elements to the DOM as you like after the event is bound and they work too. e holds a reference to the element that was clicked.

Copy this code and paste it in your HTML
  1. $('#myTable').click(function(e) {
  2. var clicked = $(e.target);
  3. clicked.css('background', 'red');
  4. });

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.