Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2018 19:44
Show Gist options
  • Select an option

  • Save anonymous/c5c33993923d319d457eb93619778211 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/c5c33993923d319d457eb93619778211 to your computer and use it in GitHub Desktop.
.some-class {
background-color: yellow;
}
<button data-toggle-class="#targetDiv:some-class">Toggle this Class on the Element</button>
<div id="targetDiv">target</div>
document.querySelectorAll('[data-toggle-class]').forEach((element) => {
element.addEventListener('click', (event) => {
event.preventDefault();
const [target, className] = element.getAttribute('data-toggle-class').split(':');
document.querySelector(target).classList.toggle(className);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment