Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

How to create collapsable using pure Javascript

const collapsableBtn = document.querySelectorAll('.collapsable-toggle');

for (let index = 0; index < collapsableBtn.length; index++) {
    collapsableBtn[index].addEventListener('click', function(e) {
        // e.preventDefault();
        e.stopImmediatePropagation();

        iterateElement = this;

        getCollapsableParent = iterateElement.parentElement;

        if(getCollapsableParent.classList.contains('show')) {
            getCollapsableParent.classList.remove('show')
            iterateElement.innerText = iterateElement.getAttribute('data-onCloseText');

        } else {
            getCollapsableParent.classList.add('show');
            iterateElement.innerText = iterateElement.getAttribute('data-onOpenText');
        }
    })
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #create #collapsable #pure #Javascript
ADD COMMENT
Topic
Name
5+7 =