Javascript Library to Toggle CSS Class for the Element By Clicking on Another Element | Class Toggler

A simple JavaScript library to toggle the CSS class of an element by clicking on another element (s). Switching is controlled via data attributes.

Suitable for creating show/hide, more/less, tabs, accordions, dropdowns, or conditional components without writing JavaScript code, all done in your HTML template.

How to make use of it:

1. Link to the Class Toggler library.

<script defer src="js/class-toggler.min.js"></script>

2. Initialize the Class Toggler and we’re ready to go.

document.addEventListener('DOMContentLoaded', () => {
  classToggler.init();
});

3. Add the ‘-hidden-toggle’ class to the component to be hidden.

<div class="-hidden-toggle" data-toggle-name="toggle-name">
  Element to be toggled
</div>

4. Create a trigger component to toggle the component.

<button data-toggle-target="toggle-name">
  Toggle The Element
</button>

5. Override the default CSS classes to be toggled.

classToggler.init({
  classActive: '-active-toggle',
  classHidden: '-hidden-toggle'
});

6. Or by way of HTML information attributes:

<div data-toggle-name="toggle-name" data-toggle-class="text--hidden">
  Element to be toggled
</div>
<button data-toggle-target="toggle-name" data-toggle-class="button--active">
  Toggle The Element
</button>

7. Create a tabs component using the data-toggle-tabs attribute:

<div class="Tabs">
  <!-- Tabs buttons -->
  <ul class="Tabs__buttons">
    <li class="Tabs__buttons-li">
      <a
        class="Tabs__buttons-link Tabs__buttons-link--active"
        data-toggle-target="tab-1"
        data-toggle-class="Tabs__buttons-link--active"
        data-toggle-tabs="tabs"
        href="#tab-1"
        >Tab one</a
      >
    </li>
    <li class="Tabs__buttons-li">
      <a
        class="Tabs__buttons-link"
        data-toggle-target="tab-2"
        data-toggle-class="Tabs__buttons-link--active"
        data-toggle-tabs="tabs"
        href="#tab-2"
        >Tab two</a
      >
    </li>
  </ul>
  <!-- Tabs contents -->
  <div
    class="Tabs__content"
    data-toggle-name="tab-1"
    data-toggle-class="Tabs__content--hidden"
    id="tab-1"
  >
    <p>
      Content of the first tab. Integer porttitor odio nibh. In non dolor sed est molestie feugiat at eu risus. Duis orci dui, viverra id suscipit sed, posuere non ante. Curabitur ac tellus non velit dictum aliquet in eu orci.
    </p>
  </div>
  <div
    class="Tabs__content Tabs__content--hidden"
    data-toggle-name="tab-2"
    data-toggle-class="Tabs__content--hidden"
    id="tab-2"
  >
    <p>
      Content of the second tab. Lorem ipsum dolor sit amet, consectetur ipiscing elit. Fusce malesuada magna nec justo placerat egestas. Integer porttitor odio nibh. In non dolor sed est molestie feugiat at eu risus. Duis orci dui, viverra id suscipit sed, posuere non ante. Curabitur ac tellus non velit dictum aliquet in eu orci.
    </p>
  </div>
</div>

Toggle CSS Class Of An Element, javascript toggle class onclick, Class Toggler Plugin/Github


See Demo And Download

Official Website(vyvrhel): Click Here

This superior jQuery/javascript plugin is developed by vyvrhel. For extra Advanced Usages, please go to the official website.

Related Posts

Iconpicker-Bootstrap-5

[Icon Picker] Iconpicker for Bootstrap 5 Icons Library

Bootstrap 5-based icon picker which supports any icon libraries like Bootstrap Icons, Font Awesome[fontawesome.com], etc. Must Read: 1000+ Pixel Perfect SVG Icons For Vue Components | Unicons How…

bootstrap-multiple-image-upload-with-preview

Bootstrap Multiple Image Upload with Preview and Delete | ImagesLoader

ImagesLoader is a standard bootstrap image upload plugin that provides an easy-to-use and nice-looking interface for uploading multiple images to a web server. Must Read: HTML 5…

Animating-Split-Flap-Displays-fallblatt

A Lightweight jQuery Plugin for Animating Split-Flap Displays | fallblatt

fallblatt is a lightweight jQuery plugin for animating split screens. This jQuery plugin allows you to include such offers in your web application. Everything from virtual departure…

bootstrap-5-dark-theme

Dark & Light Switch Mode Toggle for Bootstrap 5

Switching to dark mode is done by toggling HTML tags that include -dark or -light as a category. It is made by manipulating the DOM with JavaScript. The text color also changes depending…

jQuery-SyoTimer-Plugin

jQuery Plugin for Countdown Timer on HTML Page | SyoTimer

yoTimer jQuery plugin allows you to create digital style countdowns/periodic timers on the webpage, with callbacks support and timezone/translation customization. Features Periodic count with the specified period…

vue-js-periodic-table

Dynamic, Data-driven Periodic Table built with Vue.js

Periodicity is a dynamic, data-driven periodic table created with Vue.js that uses D3 animations and graphs to show the beauty of periodic trends. Built With vue.js (component…