18 lines
987 B
JavaScript
18 lines
987 B
JavaScript
/**
|
|
* Adds an event listener to the specified target element(s) for the given event(s), and returns a function to remove it.
|
|
* @param target The target element(s) to add the event listener to.
|
|
* @param event The event(s) to listen for.
|
|
* @param handler The function to be called when the event is triggered.
|
|
* @param options An optional object that specifies characteristics about the event listener.
|
|
* @returns A function that removes the event listener from the target element(s).
|
|
*/
|
|
export function addEventListener(target, event, handler, options) {
|
|
const events = Array.isArray(event) ? event : [event];
|
|
// Add the event listener to each specified event for the target element(s).
|
|
events.forEach((_event) => target.addEventListener(_event, handler, options));
|
|
// Return a function that removes the event listener from the target element(s).
|
|
return () => {
|
|
events.forEach((_event) => target.removeEventListener(_event, handler, options));
|
|
};
|
|
}
|