Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active June 23, 2017 07:25
Show Gist options
  • Save chriswrightdesign/845f3de86941e659f52e8530900572d3 to your computer and use it in GitHub Desktop.
Save chriswrightdesign/845f3de86941e659f52e8530900572d3 to your computer and use it in GitHub Desktop.
Closure event listener
/**
* Imagine we had an event listener that would control toggling a visibility class of an element
**/
const toggleVisibleClass = event => {
console.log(event.target.value + 'changed');
someTarget.classList.toggle('is-visible');
}
checkbox.addEventListener('change', toggleVisible, false);
/**
* We could pass the dom node in that we want to change,
* this enables us to keep using this function on different checkboxes
**/
const toggleVisibleClass = domNode => event => {
domNode.classList.toggle('is-visible');
}
const myForm = document.querySelector('.the-form');
checkbox.addEventListener('change', toggleVisibleClass(myForm), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment