cferdinandi / tabby

Lightweight, accessible vanilla JS toggle tabs.
MIT License
602 stars 73 forks source link

Toggle Tabby - Quick and dirty solution #78

Closed jenstornell closed 7 years ago

jenstornell commented 7 years ago

I needed to be able to toggle when clicking on the same tab twice. I made a solution for it. In case someone needs the same thing, here it is:


document.addEventListener("DOMContentLoaded", function(event) {
    var tabby_active;
    tabby.init({
        callback: function ( tabs, toggle ) {
            var active_pane = document.querySelector('.tabs-pane.active');
            var active_tab = document.querySelector('.tabs .active');

            if( tabby_active == toggle ) {
                active_pane.classList.remove('active');
                active_tab.classList.remove('active');
                active_tab.querySelector('.active').classList.remove('active');
                tabby_active = '';
            } else {
                tabby_active = '#' + active_pane.getAttribute('id');
            }
        }
    });
});