Events API
evt.add()
asyncAdds an event listener that calls a specified function whenever the event is dispatched to the target.
-
An event listener can be attached to a Window, Document, Element, array of elements, NodeList, or HTMLCollection.
If no target is specified, automatically detects the correct target. -
Accepts a type of event to listen for, provided as a case-sensitive string, as the first argument.
Accepts a function (or an object with a handleEvent method) that will be called when the specified event occurs, as the second argument.
Accepts an optional third argument, which can be one of the following:ArgumentDescriptiontrueThe handler will be invoked during the capture phase (before the event reaches its target).falseThe handler will be invoked during the bubbling phase (after the event has passed through the capture phase).
This is the default value.nullThe handler will be executed before the default action for the specified event is performed.objectAn object that defines the characteristics of an event listener, including the following options:OptionTypeDefaultDescriptioncapturebooleanfalseSpecifies whether the handler is invoked during the capture phase (true) or the bubbling phase (false).oncebooleanfalseSpecifies that the listener will be called at most once after being added and automatically removed after the first call, if set to true.passivebooleanfalseSpecifies that the listener's function will never call preventDefault(), if set to true.signalAbortSignalnoneAllows the handler to be removed using abort().The event types "visible" and "invisible" include the following options:OptionTypeDefaultDescriptionrootDOM elementnullThe element used as the viewport for observing intersections. Defaults to the browser viewport if set to null.rootMarginstring0px 0px 0px 0pxMargins around the root (in CSS syntax). Expands or shrinks the effective viewport for intersection calculations.thresholdnumber
array0A single number or array of numbers (0 to 1) indicating at what percentage of the target's visibility the observer's callback should be triggered.The event type "mutation" includes the following options:OptionTypeDefaultDescriptionsubtreebooleantrueSet to true to enable monitoring of the entire subtree of nodes rooted at the target.
All properties will apply to the entire subtree rather than just the target node.
Changes in a descendant subtree will still be observed even after the descendant is removed, until the removal notification is delivered.childListbooleantrueSet to true to monitor the target node (and its descendants, if subtree is true) for added or removed child nodes.attributesbooleantrueSet to true to monitor attribute value changes on the observed nodes.attributeFilterarraynoneAn array of attribute names to monitor, if omitted, all attribute changes trigger notifications.attributeOldValuebooleantrueSet to true to capture the previous value of any changing attribute while monitoring for attribute changes on nodes.characterDatabooleantrueSet to true to monitor the target node (and its descendants, if subtree is true) for changes to its character data.characterDataOldValuebooleantrueSet to true to capture the previous text value whenever it changes on monitored nodes. -
The event handler receives a standard event object with the following additional properties:
PropertyTypeDescriptionitDOM elementThe element the event was assigned to.
While "target" points to the specific element that triggered the event, "it" references the assigned element.
If an event is bound to a <div> and triggered on a child <span>, "target" will be <span>, and "it" will be <div>.itTypestringThe actual type of the event.
For example, for a "click-out" event, the "type" property will be "click", while the "itType" property will represent "click-out". - It is not necessary to wait for the method to be executed using await if not needed.
Example:
// Adds an event listener to the Document.
document.evt.add('click', (e) => {});
// Adds an event listener to the Window.
window.evt.add('scroll', (e) => {});
// Adds an event listener to the <div> element.
$('div').evt.add('click', (e) => {});
// Adds an event listener to all <div> elements.
$('div ?').evt.add('click', (e) => {});
// Automatically detects the correct target and adds an event listener to it, in this case, the Document.
evt.add('click', (e) => {});
// Automatically detects the correct target and adds an event listener to it, in this case, the Window.
evt.add('scroll', (e) => {});
// The event handler is invoked during the capture phase.
evt.add('click', (e) => {}, true);
evt.add('click', (e) => {}, {capture: true});
// Adds an event listener with the specified options to the <div> element.
$('div').evt.add('click', (e) => {}, {
capture: false,
once: true,
passive: false
});
// Adds a mutation event listener to all elements.
evt.add('mutation', (e) => {}, {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
// Adds a mutation event listener to a <div> element.
$('div').evt.add('mutation', (e) => {});
// Adds a resize event listener to the window object.
evt.add('resize', (e) => {});
// Adds a resize event listener to a <div> element.
$('div').evt.add('resize', (e) => {});
// The event handler is triggered whenever the <div> element becomes visible within the viewport.
$('div').evt.add('visible', (e) => {}, {
root: null,
rootMargin: '0px 0px 0px 0px',
threshold: 0
});
// The event handler is triggered whenever the <div> element becomes invisible within the viewport.
$('div').evt.add('invisible', (e) => {});
// Waits for the event listener to be added to the target.
await evt.add('click', (e) => {});