delegate
Lightweight event delegation
Last updated 8 years ago by zenorocha .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install delegate 
SYNC missed versions from official npm registry.

delegate

Lightweight event delegation.

Install

You can get it on npm.

npm install delegate --save

If you're not into package management, just download a ZIP file.

Setup

Node (Browserify)
var delegate = require('delegate');
Browser (Standalone)
<script src="dist/delegate.js"></script>

Usage

Add event delegation

With the default base (document)

delegate('.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

With an element as base

delegate(document.body, '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

With a selector (of existing elements) as base

delegate('.container', '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

With an array/array-like of elements as base

delegate(document.querySelectorAll('.container'), '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

Remove event delegation

With a single base element (default or specified)

var delegation = delegate(document.body, '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

delegation.destroy();

With multiple elements (via selector or array)

Note: selectors are always treated as multiple elements, even if one or none are matched. delegate() will return an array.

var delegations = delegate('.container', '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

delegations.forEach(function (delegation) {
    delegation.destroy();
});

Browser Support

Chrome logo Edge logo Firefox logo Internet Explorer logo Opera logo Safari logo
Latest ✔ Latest ✔ Latest ✔ 9+ ✔ Latest ✔ Latest ✔

License

MIT License © Zeno Rocha

Current Tags

  • 3.2.0                                ...           latest (7 years ago)

14 Versions

  • 3.2.0                                ...           7 years ago
  • 3.1.3                                ...           7 years ago
  • 3.1.2                                ...           8 years ago
  • 3.1.1                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.0.1                                ...           12 years ago
Maintainers (1)
Downloads
Total 11
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (12)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE