Skip to content

Instantly share code, notes, and snippets.

@davidsheardown
Created December 26, 2021 13:58
Show Gist options
  • Save davidsheardown/a01a36dfa51f42a6c88c88baa2d04abc to your computer and use it in GitHub Desktop.
Save davidsheardown/a01a36dfa51f42a6c88c88baa2d04abc to your computer and use it in GitHub Desktop.
Javascript version of jQuery live (dynamic element) binding
function jslive(eventType, elementId, cb) {
document.addEventListener(eventType, function (event) {
if (event.target.id === elementId) {
cb.call(event.target, event);
}
});
}
// Params are: event type (click, mouseover etc), element to look for (id or class etc), callback function
jslive("click", ".my-class", function (event) {
console.log("element fired", event);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment