Posted over 9 years ago. Visible to the public. Repeats.

Efficiently add a click event handler to many elements in jQuery

When you need to add a click event handler to 50+ elements in jQuery, this might slow down the browser.

You can simply register an event at the root of the DOM instead. This waits for events to bubble up and checks whether the triggering element matches the selector:

Copy
$(document).on('click', '.dialog_link', function(event) { // Open dialog });

Fun fact: Angular 2.0 will provide native syntax for event handlers that are registered on the document body,

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 5 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more