Menu

Toggler

Added in 1.8.0

Toggler switches a class or an attribute on click. It needs only data attributes, so you do not have to write small one-off scripts.

Overview

Add data-bs-toggle="toggler" to an element and put the class name in data-bs-value. Each click adds the class, and the next click removes it.

<button type="button" class="btn" data-bs-toggle="toggler" data-bs-value="btn-primary">Toggle btn-primary</button>

Usage

Remove an existing class

If the element already has the class, the first click removes it.

<button type="button" class="btn btn-primary" data-bs-toggle="toggler" data-bs-value="btn-primary">Starts with btn-primary</button>

Target element

Use data-bs-target to change another element. Put data-bs-value on the target, not on the button.

The button toggles bg-primary-lt here.
<button type="button" class="btn mb-3" data-bs-toggle="toggler" data-bs-target="#toggler-target">Toggle</button>
<div id="toggler-target" class="p-3 rounded border" data-bs-value="bg-primary-lt">The button toggles bg-primary-lt here.</div>

Multiple elements

When the selector in data-bs-target matches many elements, Toggler changes every one of them. Each one can have its own value.

Toggles bg-blue-lt
Toggles bg-green-lt
<button type="button" class="btn mb-3" data-bs-toggle="toggler" data-bs-target="[data-toggler-demo]">Toggle all</button>
<div class="p-3 mb-2 rounded border" data-toggler-demo="true" data-bs-value="bg-blue-lt">Toggles bg-blue-lt</div>
<div class="p-3 rounded border" data-toggler-demo="true" data-bs-value="bg-green-lt">Toggles bg-green-lt</div>

Toggle an attribute

Set data-bs-attribute to toggle an attribute instead of a class. The attribute gets the value from data-bs-value, and the next click removes the attribute.

Now you see me.
<button type="button" class="btn mb-3" data-bs-toggle="toggler" data-bs-target="#toggler-hidden">Show or hide</button>
<div id="toggler-hidden" class="p-3 rounded border" data-bs-attribute="hidden" data-bs-value="true">Now you see me.</div>

You can use the same pattern to toggle disabled on a fieldset and lock all controls inside it. Every attribute works except id.

Data attributes

Attribute Description
data-bs-toggle="toggler" Turns the element into a trigger.
data-bs-target Optional CSS selector of the element to change. Without it, the trigger changes itself.
data-bs-attribute Attribute to toggle. Default is class.
data-bs-value Class name or attribute value to toggle. You can give several class names separated by spaces. Without it, nothing happens.

JavaScript

Toggler is made to avoid writing JavaScript, but you can also use it from code.

const toggler = tabler.Toggler.getOrCreateInstance('#my-element', {
  attribute: 'class',
  value: 'active',
})

toggler.toggle()
Method Description
toggle() Toggles the class or the attribute.
dispose() Removes the instance from the element.
getInstance() Static. Returns the instance of an element, or null.
getOrCreateInstance() Static. Returns the instance of an element, or creates one.

Events

Event Description
toggle.bs.toggler Fires before the change. Call event.preventDefault() to stop it.
toggled.bs.toggler Fires after the change.

Both events fire on the element that changes. With data-bs-target, this is the target, not the trigger.

document.getElementById('my-element').addEventListener('toggled.bs.toggler', () => {
  // do something
})

Accessibility

  • Use a <button> as the trigger, so it works with the keyboard.
  • Toggler does not change ARIA attributes. If the change shows or hides content, keep aria-expanded or aria-pressed in sync in a toggled.bs.toggler listener, or use Accordion instead.
  • A trigger with the disabled attribute or the .disabled class does nothing.