Skip to content

Instantly share code, notes, and snippets.

@rslahmed
Last active March 15, 2020 08:37
Show Gist options
  • Save rslahmed/a2ae92f1d7ba7882b43170e75d513420 to your computer and use it in GitHub Desktop.
Save rslahmed/a2ae92f1d7ba7882b43170e75d513420 to your computer and use it in GitHub Desktop.
accordion one open only
var acc = document.getElementsByClassName("sh-box-content");
var panel = document.getElementsByClassName('sh-details');
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment