-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-toggle.js
34 lines (33 loc) · 1.34 KB
/
js-toggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Generic .js-toggle using data-element and data-group. Use .js-active-default if needed in responsive design
function ready(event) {
function addEventListenerList(list, event, fn) {
for (var i = 0, len = list.length; i < len; i++) {
list[i].addEventListener(event, fn, false);
}
}
function handleClick() {
var dataElement = this.dataset.element;
var group = document.getElementsByClassName(this.dataset.group);
var element = document.getElementById(dataElement);
if (element.className === 'js-active-default') {
element.classList.remove('js-active-default');
} else {
if (!element.className === 'js-active') {
if (group) {
Array.prototype.forEach.call(group, function(el) {
el.classList.remove('js-active');
});
}
element.classList.toggle('js-active');
} else {
element.classList.toggle('js-active');
}
}
}
var listJsToggle = document.getElementsByClassName('js-toggle');
addEventListenerList(listJsToggle, 'click', handleClick);
// clean up event binding
window.removeEventListener('DOMContentLoaded', ready);
}
// bind to the load event
window.addEventListener('DOMContentLoaded', ready);