toggle-offToggle API

Global API (window.Toggles)

The component exposes a global registry to interact with toggle instances programmatically.

window.Toggles.getAll()

Returns an array of all initialized Toggle class instances on the page.

window.Toggles.findById(id)

Returns the Toggle instance with the matching button ID.

window.Toggles.findByTarget(targetId)

Returns an array of Toggle instances that target the specified element ID.


Instance API (Toggle)

Each toggle instance provides properties and methods for state control.

Properties

  • id: The unique ID of the toggle (generated if not present on the button).

  • button: The HTML button element.

  • target: The HTML target element (resolved at runtime).

  • isEnabled: Boolean indicating if the toggle is currently active (supports getter and setter).

  • config: The parsed configuration object (read-only via getConfig()).

Methods

  • enable(): Activates the toggle.

  • disable(): Deactivates the toggle.

  • toggle(): Flips the current state.

  • setEnabled(boolean): Explicitly sets the state.

  • updateLabelWidth(): Force recalculation of label widths (if in alternate mode).

  • on(eventName, handler): Adds an event listener to the target element.

  • off(eventName, handler): Removes an event listener from the target element.

  • getConfig(): Returns a copy of the parsed configuration from data attributes.


Events

Events are dispatched from the target element. They bubble up and are not cancelable.

Event Name
Detail
Description

toggle:enable

{ id, isEnabled: true }

Fired when the toggle is activated.

toggle:disable

{ id, isEnabled: false }

Fired when the toggle is deactivated.

toggle:change

{ id, isEnabled: boolean }

Fired on any state change.


Examples

Programmatic Toggle Control

Finding Toggles by Target

Event Listeners for State Changes

Using Instance Event Methods

Reading Toggle Configuration

Close All Open Toggles

Last updated