| 1 |
# delegate |
| 2 |
|
| 3 |
Lightweight event delegation. |
| 4 |
|
| 5 |
## Install |
| 6 |
|
| 7 |
You can get it on npm. |
| 8 |
|
| 9 |
``` |
| 10 |
npm install delegate --save |
| 11 |
``` |
| 12 |
|
| 13 |
If you're not into package management, just [download a ZIP](https://github.com/zenorocha/delegate/archive/master.zip) file. |
| 14 |
|
| 15 |
## Setup |
| 16 |
|
| 17 |
###### Node (Browserify) |
| 18 |
|
| 19 |
```js |
| 20 |
var delegate = require('delegate'); |
| 21 |
``` |
| 22 |
|
| 23 |
###### Browser (Standalone) |
| 24 |
|
| 25 |
```html |
| 26 |
<script src="dist/delegate.js"></script> |
| 27 |
``` |
| 28 |
|
| 29 |
## Usage |
| 30 |
|
| 31 |
### Add event delegation |
| 32 |
|
| 33 |
#### With the default base (`document`) |
| 34 |
|
| 35 |
```js |
| 36 |
delegate('.btn', 'click', function(e) { |
| 37 |
console.log(e.delegateTarget); |
| 38 |
}, false); |
| 39 |
``` |
| 40 |
|
| 41 |
#### With an element as base |
| 42 |
|
| 43 |
```js |
| 44 |
delegate(document.body, '.btn', 'click', function(e) { |
| 45 |
console.log(e.delegateTarget); |
| 46 |
}, false); |
| 47 |
``` |
| 48 |
|
| 49 |
#### With a selector (of existing elements) as base |
| 50 |
|
| 51 |
```js |
| 52 |
delegate('.container', '.btn', 'click', function(e) { |
| 53 |
console.log(e.delegateTarget); |
| 54 |
}, false); |
| 55 |
``` |
| 56 |
|
| 57 |
#### With an array/array-like of elements as base |
| 58 |
|
| 59 |
```js |
| 60 |
delegate(document.querySelectorAll('.container'), '.btn', 'click', function(e) { |
| 61 |
console.log(e.delegateTarget); |
| 62 |
}, false); |
| 63 |
``` |
| 64 |
|
| 65 |
### Remove event delegation |
| 66 |
|
| 67 |
#### With a single base element (default or specified) |
| 68 |
|
| 69 |
```js |
| 70 |
var delegation = delegate(document.body, '.btn', 'click', function(e) { |
| 71 |
console.log(e.delegateTarget); |
| 72 |
}, false); |
| 73 |
|
| 74 |
delegation.destroy(); |
| 75 |
``` |
| 76 |
|
| 77 |
#### With multiple elements (via selector or array) |
| 78 |
|
| 79 |
Note: selectors are always treated as multiple elements, even if one or none are matched. `delegate()` will return an array. |
| 80 |
|
| 81 |
```js |
| 82 |
var delegations = delegate('.container', '.btn', 'click', function(e) { |
| 83 |
console.log(e.delegateTarget); |
| 84 |
}, false); |
| 85 |
|
| 86 |
delegations.forEach(function (delegation) { |
| 87 |
delegation.destroy(); |
| 88 |
}); |
| 89 |
``` |
| 90 |
|
| 91 |
## Browser Support |
| 92 |
|
| 93 |
| <img src="https://clipboardjs.com/assets/images/chrome.png" width="48px" height="48px" alt="Chrome logo"> | <img src="https://clipboardjs.com/assets/images/edge.png" width="48px" height="48px" alt="Edge logo"> | <img src="https://clipboardjs.com/assets/images/firefox.png" width="48px" height="48px" alt="Firefox logo"> | <img src="https://clipboardjs.com/assets/images/ie.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="https://clipboardjs.com/assets/images/opera.png" width="48px" height="48px" alt="Opera logo"> | <img src="https://clipboardjs.com/assets/images/safari.png" width="48px" height="48px" alt="Safari logo"> | |
| 94 |
|:---:|:---:|:---:|:---:|:---:|:---:| |
| 95 |
| Latest ✔ | Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | Latest ✔ | |
| 96 |
|
| 97 |
## License |
| 98 |
|
| 99 |
[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha |