Lightweight event delegation
npm install delegate-it
const delegate = require('delegate-it');
import delegate from 'delegate-it';
delegate('.btn', 'click', event => {
console.log(event.delegateTarget);
});
delegate(document.body, '.btn', 'click', event => {
console.log(event.delegateTarget);
});
delegate('.container', '.btn', 'click', event => {
console.log(event.delegateTarget);
});
delegate(document.querySelectorAll('.container'), '.btn', 'click', event => {
console.log(event.delegateTarget);
});
const delegation = delegate(document.body, '.btn', 'click', event => {
console.log(event.delegateTarget);
});
delegation.destroy();
Note: selectors are always treated as multiple elements, even if one or none are matched. delegate()
will return an array.
const delegations = delegate('.container', '.btn', 'click', event => {
console.log(event.delegateTarget);
});
delegations.forEach(function (delegation) {
delegation.destroy();
});
Latest ✔ | Latest ✔ | Latest ✔ | No ✕ | Latest ✔ | Latest ✔ |