Events API

evt.toggle()

async

Dynamically enables all events, or specific events passed to it, either globally or for matched DOM elements if they are not already enabled, and disables them if they are currently enabled.

  • Accepts optional arguments as strings, arrays of strings, and a boolean value (true or false):
    Argument
    Description
    true
    Enables events.
    false
    Disables events.
    You can pass event names as individual arguments, as an array, or as a string separated by commas or spaces.
    Each value is seen as a new argument.
  • Returns a value for a single matched DOM element and a single passed event name: true if the event was eventually enabled, and false otherwise.
    If no DOM elements are matched and a single event name is passed, it returns true if the event was eventually enabled, and false otherwise.
    If no DOM elements are matched and multiple event names are passed, an object is returned where the keys are the event names, and the values represent their final state: true if the event is enabled, and false otherwise.
  • It is not necessary to wait for the method to be executed using await if not needed.
Example:
// Toggles the events that are passed.
await evt.toggle('click');
// true | false

await evt.toggle('click', 'input');
await evt.toggle(['click', 'input']);
await evt.toggle('click, input');
await evt.toggle('click input');
// {click: true, input: false}

// Enables the events that are passed.
await evt.toggle('click', true);
// true

// Disables the events that are passed.
await evt.toggle('click', false);
// false

// Toggles all events on the <div> element.
await $('div').evt.toggle();
// true | false

// Toggles all events on all <div> elements.
await $('div ?').evt.toggle();
// undefined

// Toggles the events that are passed on the <div> element.
await $('div').evt.toggle('click');
// true | false

await $('div').evt.toggle('click', 'input');
// undefined

// Toggles the events that are passed on all <div> elements.
await $('div ?').evt.toggle('click', 'input');
// undefined