Last active
June 23, 2017 07:25
-
-
Save chriswrightdesign/845f3de86941e659f52e8530900572d3 to your computer and use it in GitHub Desktop.
Closure event listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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