Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$.live() like function in pure js
var live = (function () {
var eventRegistry = {};
function dispatchEvent(event) {
var targetElement = event.target;
eventRegistry[event.type].forEach(function (entry) {
var potentialElements = document.querySelectorAll(entry.selector);
var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
if (hasMatch) {
entry.handler.call(targetElement, event);
}
});
}
return function (selector, event, handler) {
if (!eventRegistry[event]) {
eventRegistry[event] = [];
document.documentElement.addEventListener(event, dispatchEvent, true);
}
eventRegistry[event].push({
selector: selector,
handler: handler
});
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.