Event delegation library on plain JavaScript.
Simple, small only 1.2kb compressed and about 620 bytes gzipped.
Tested on latest Chrome, Firefox, Safari, Opera, IE9+.
You can use Dega as global object, AMD or CommonJS module.
bower install dega --save
npm install dega --save
Dega = require('dega');
var handler = function(e) {
e.preventDefault();
console.log('event');
};
// Subscribe
Dega(document).on('click', '.link', handler);
// Unsubscribe
Dega(document).off('click', '.link', handler);
Dega is a self-invoking constructor, that means you shouldn't call it with new
keyword.
Dega constructor accepts 2 kind of arguments:
- HTMLElement
Dega(document)
- String — selector which will be used in document.querySelector:
Dega('.class')
Dega('#id')
Dega('tag')
Dega vs jQuery 2.1.3 comparsion — jsperf.
I don't know how long link will work, so there are some screenshots:
- Remove All Events by Selector
- Remove All Events by Type
- Add more test cases