This module installs singular event handlers on the document, and evaluates which callback to call when the event fires, based on the element the event originates from. This allows us to swap out and rerender whole sections of the DOM without having to reinstall a bunch of event handlers each time. This nicely decouples the render logic from the event management logic.
- Source:
Example
import ge from "./global-events.js";
ge.addEventListener("button#click-me", "click", (event, element) => {
console.log(event); // Will give back the click event object
console.log(element); // Will give back the button#click-me element that was clicked
});
Methods
(static) addEventListener(selector, event, handler)
Add a global event listener
Parameters:
Name | Type | Description |
---|---|---|
selector |
string | CSS selector of the element(s) to trigger on |
event |
string | What event to trigger on |
handler |
eventListener | The function to call when |
- Source:
Type Definitions
eventListener(event, element)
Handle a global event
Parameters:
Name | Type | Description |
---|---|---|
event |
Event | The event that triggered the eventListener |
element |
HTMLElement | The element that matched the selector |
- Source: