Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active August 29, 2015 14:01
Show Gist options
  • Save cferdinandi/4e7ad4da839fefa33c05 to your computer and use it in GitHub Desktop.
Save cferdinandi/4e7ad4da839fefa33c05 to your computer and use it in GitHub Desktop.
A simple method to set and remove event listeners for dynamically generated content.
// Event selector defaults
var toggles; // Toggle nodes list
var eventListeners = []; //Listeners array
// Whenever a toggle is clicked, run the function
toggles = document.querySelectorAll('[data-collapse]'); // Get all collapse toggles
forEach(toggles, function (toggle, index) {
eventListeners[index] = exports.FUNCTION.bind( null, VARIABLES );
toggle.addEventListener('click', eventListeners[index], false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment