Events

Reference Function Description
# on Adds a removable event listener to any HTMLElement.
# off Removes an event listener from any HTMLElement.
# clear_event_listeners Removes all event listeners from an element and all it's children.
# collect_garbage Removes all event listeners from elements no longer in the live DOM.
# event_listeners Returns an array of all attached event listeners on element.

on

Adds a removable event listener callback to an HTMLElement. Event names can be native browser events or custom event names.

on(HTMLElement: element, String: event, Function: callback, ?Array|Mixed: args): Void
// Anonymous function
on(element, 'click', (event, element) => console.log(event, element));

// Multiple event can also be added
on(element, 'click, touchstart', (event, element) => console.log(event, element));

// Regular function
on(element, 'click', function(event, element)
{
    console.log(event, element, this);
});

// Function with "this" applied
on(element, 'click', function(event, element)
{
    console.log(event, element, this);

}, 'foo');

// Function with "this" applied and arguements
on(element, 'click', function(event, element, one, two)
{
    // this -> 'foo'
    console.log(event, element, this);

}, ['foo', 'one', 'two']);


off

Removes an event listener from any HTMLElement.

off(HTMLElement: element, String: event, Function: callback, ?Array|Mixed: args): Void
// Add the event listener
let func = (event, element) => console.log(event, element);
on(element, 'click', func);

// Removes specific event listener
off(element, 'click', func);

// Removes all 'click' listeners
off(element, 'click');

// Removes all event listeners
off(element);


clear_event_listeners

Removes all event listeners from an element and all it's children.

clear_event_listeners(HTMLElement: element, Boolean: onlyChildren = false): Void
// Removes all listeners including on any children
clear_event_listeners(element);

// Removes all listeners from children but not parent
clear_event_listeners(element, true);


collect_garbage

Removes all event listeners from elements no longer in the live DOM.

collect_garbage(): Void


event_listeners

Returns an array of all attached event listeners on element.

event_listeners(HTMLElement|String: element, ?String: eventName): array
// [ { el: details.element, callback: details.callback, type: type } ]

// Returns all event listeners
let events = event_listeners();

// Returns all 'click' event listeners
let events = event_listeners('click');

// Returns all event listeners on element
let events = event_listeners(element);

// Returns all 'click' event listeners on element
let events = event_listeners(element, 'click');