Skip to content

Instantly share code, notes, and snippets.

@laphilosophia
Created January 18, 2017 11:02
Show Gist options
  • Save laphilosophia/debcd794a5eec8905e7f088633a2e493 to your computer and use it in GitHub Desktop.
Save laphilosophia/debcd794a5eec8905e7f088633a2e493 to your computer and use it in GitHub Desktop.
var evts = (function () {
'use strict';
var listeners = [],
add,
remove;
add = function (evt, elem, fn) {
var callback = function (evt) {
fn(evt.target);
};
listeners.push({
evt: evt,
elem: elem,
fn: fn,
callback: callback,
removed: false
});
elem.addEventListener(evt, callback);
};
remove = function (evt, elem, fn) {
var callback;
each(listeners, function (evtObj) {
if (evtObj.evt === evt && evtObj.elem === elem && evtObj.fn === fn && !evtObj.removed) {
evtObj.removed = true;
callback = evtObj.callback;
return false;
}
});
elem.removeEventListener(evt, callback);
};
return {
add: add,
remove: remove
};
}());
var callback = function (evt, target) {
console.log(target);
};
evts.add('click', document, callback);
evts.remove('click', document, callback);
var root = document.getElementByTagName('element');
evts.add('click', root, function(message){
root.innerHTML = message;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment