Namespace: Events:off

Events:off

Source:

Methods

off(targets, events, handleropt, nullable, tryNativeRemovalopt, nullable) → {Number}

Removes (a), previously defined, event listener(s) on (a) valid EventTarget(s) (most likely (a) DOM-element(s)).

The definition of targets and events works exactly as in "on" and "once", the only difference being, that the handler is optional in this case, which results in the removal of all handlers, without targeting a specific one.

To specifically target handlers without a namespace, please use the namespace-string "__default".

This function does not differentiate between removal of capture/non-capture events, but always removes both.

If you try to remove event handlers not previously created with on (and therefore there are no fitting target entries in the EVENT_MAP), the function will fall back to native removeEventListener (if tryNativeRemoval is true), but in that case, a handler has to be defined and the return value will not increment, since we do not know if the removal really worked.

Parameters:
Name Type Attributes Default Description
targets EventTarget | Array.<EventTarget>

the target(s) to remove event handlers from

events String | Array.<String>

the event name(s) to remove, can be either a single name or a list of names, each name may also have a namespace, separated by a dot, to target all events/namespaces, you may use ""/".*"

handler function <optional>
<nullable>
null

a specific callback function to remove

tryNativeRemoval Boolean <optional>
<nullable>
true

if a target is not part of the EVENT_MAP native removeEventListener is used as a fallback if this is true (handler needs to be set in that case)

Source:
See:
Throws:
  • error in case no targets are defined

  • error in case no events are defined

  • error in case a defined handler is not a function

  • error in case targets are not all usable event targets

  • error in case delegations are missing viable ancestor targets

Returns:
the number of handlers actually removed by the function call, may also be 0 if nothing matched
Type
Number
Example
off(buttonElement, 'click');
off(bar, '*.__default');
off(customEventElement, 'crash');
off([ancestorElement, 'a'], 'click');
off([ancestorElement, '.btn[data-foobar="test"]'], '*.delegated', fSpecificHandler);
off(linkElement, '*', fSpecificHandler);
off(customEventElement, ['*.test', '*.site']);
off([ancestorElement, 'a', ancestorElement, '.btn[data-foobar="test"]'], '*.*', fSpecificHandler);
off(buttonElement, '*.*');