Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Created August 24, 2016 09:53
Show Gist options
  • Select an option

  • Save kvendrik/d2545924bd76edefff07e0819606ddfa to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/d2545924bd76edefff07e0819606ddfa to your computer and use it in GitHub Desktop.
dataToggle.js
const dataToggle = {
init(){
this._els = document.querySelectorAll('[data-toggle]') || [];
this._bindEvents();
},
_bindEvents(){
[].forEach.call(this._els, (el) => {
el.addEventListener('click', () => this._toggleTarget(el), false);
});
},
_toggleTarget(el){
const targetSelector = el.getAttribute('data-toggle-target');
const className = el.getAttribute('data-toggle');
const targetEl = document.querySelector(targetSelector);
targetEl.classList.toggle(className);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment