Delegated event listeners
;// Listen for browser-generated events.;// Listen for custom events triggered by your app.;// Dispatch a custom event on an element.var image = document;;// Stop listening for event type, selector and handler.;// Stop listening for event type and selector.;// Stop listening for event type for all selectors.;
The standard method of registering event handler functions is to directly bind the listener to the element.
// Find an element and bind a function directly to it.var button = document;button;
If we have several clickable elements, listeners can be directly registered on them in a loop.
// Find all the buttons and attach a function to each of them.var buttons = document;buttons;
Directly binding event listeners to elements works great if the page doesn't change after it's initially loaded. However, if we dynamically add another button to the document, it won't receive a click event.
// No click handler is registered on the new element.var button = document;buttontextContent = 'Push';var list = document;list;
A solution to this is to delegate the event handler up the tree to the parent element that contains all of the button children. When a button is clicked, the event bubbles up the tree until it reaches the parent, at which point the handler is invoked.
// Event handling delegated to a parent element.var list = document;list;
However, now anything clicked inside the list will trigger this event
handler, not just clicks on buttons. So we add a selector check to determine
button generated the click event, rather than a
span element, text, etc.
// Filter events by matching the element with a selector.var list = document;list;
Now we have something that works for any button element inside the list whether it was included in the initial HTML page or added dynamically to the document sometime later.
But what if the list element is added to the page dynamically?
If we delegate most events up to the global
document, we no longer worry
about when elements are appended to the page—they will receive event listeners
// Delegated click handling up to global document.document;
Globally delegated events
Now that we've covered how browsers handle directly-bound and delegated events natively, let's look at what this library actually does.
The goals of this library are to:
- Provide shortcuts that make this common delegation pattern easy to use in web applications with hundreds of event listeners.
- Use the browser's native event system.
- Speed 🐎
Delegated event handling shortcuts (
fire) are provided
so event handlers aren't required to test for matching elements
themselves. jQuery has great documentation on event delegation with selectors too.
Here's the same globally delegated handler as above but using
// Easy :);
To provide compatibility with older browsers, jQuery uses "synthetic" events. jQuery listens for the native browser event, wraps it inside a new event object, and proxies all function calls, with modifications, through to the native object.
All browsers now share a standard event system, so we can remove the extra layer of event handling to recover performance.
so it won't significantly increase download times (minified + gzip = 640 bytes).
It relies on a small
data structure to optimize selector matching against the delegated events.
A micro-benchmark to compare relative event handling performance is included
and can be run with
npm run bench.
Triggering custom events
fire shortcut function is provided to trigger custom events with
attached data objects.
;var input = document;;
The standard way of doing this works well but is more verbose.
document;var input = document;input;
- Safari 6+
- Internet Explorer 9+
- Microsoft Edge
npm run bootstrap npm test npm run bench npm run browser
Distributed under the MIT license. See LICENSE for details.