| 1 |
# good-listener |
| 2 |
|
| 3 |
[](https://travis-ci.org/zenorocha/good-listener) |
| 4 |
|
| 5 |
> A more versatile way of adding & removing event listeners. |
| 6 |
|
| 7 |
 |
| 8 |
|
| 9 |
## Install |
| 10 |
|
| 11 |
You can get it on npm. |
| 12 |
|
| 13 |
``` |
| 14 |
npm install good-listener --save |
| 15 |
``` |
| 16 |
|
| 17 |
Or bower, too. |
| 18 |
|
| 19 |
``` |
| 20 |
bower install good-listener --save |
| 21 |
``` |
| 22 |
|
| 23 |
If you're not into package management, just [download a ZIP](https://github.com/zenorocha/good-listener/archive/master.zip) file. |
| 24 |
|
| 25 |
## Setup |
| 26 |
|
| 27 |
###### Node (Browserify) |
| 28 |
|
| 29 |
```js |
| 30 |
var listen = require('good-listener'); |
| 31 |
``` |
| 32 |
|
| 33 |
###### Browser (Standalone) |
| 34 |
|
| 35 |
```html |
| 36 |
<script src="dist/good-listener.js"></script> |
| 37 |
``` |
| 38 |
|
| 39 |
## Usage |
| 40 |
|
| 41 |
### Add an event listener |
| 42 |
|
| 43 |
By passing a string selector [(see full demo)](https://github.com/zenorocha/good-listener/blob/master/demo/selector.html). |
| 44 |
|
| 45 |
```js |
| 46 |
listen('.btn', 'click', function(e) { |
| 47 |
console.log(e); |
| 48 |
}); |
| 49 |
``` |
| 50 |
|
| 51 |
Or by passing a HTML element [(see full demo)](https://github.com/zenorocha/good-listener/blob/master/demo/node.html). |
| 52 |
|
| 53 |
```js |
| 54 |
var logo = document.getElementById('logo'); |
| 55 |
|
| 56 |
listen(logo, 'click', function(e) { |
| 57 |
console.log(e); |
| 58 |
}); |
| 59 |
``` |
| 60 |
|
| 61 |
Or by passing a list of HTML elements [(see full demo)](https://github.com/zenorocha/good-listener/blob/master/demo/nodelist.html). |
| 62 |
|
| 63 |
```js |
| 64 |
var anchors = document.querySelectorAll('a'); |
| 65 |
|
| 66 |
listen(anchors, 'click', function(e) { |
| 67 |
console.log(e); |
| 68 |
}); |
| 69 |
``` |
| 70 |
|
| 71 |
### Remove an event listener |
| 72 |
|
| 73 |
By calling the `destroy` function that returned from previous operation [(see full demo)](https://github.com/zenorocha/good-listener/blob/master/demo/destroy.html). |
| 74 |
|
| 75 |
```js |
| 76 |
var listener = listen('.btn', 'click', function(e) { |
| 77 |
console.log(e); |
| 78 |
}); |
| 79 |
|
| 80 |
listener.destroy(); |
| 81 |
``` |
| 82 |
|
| 83 |
## Browser Support |
| 84 |
|
| 85 |
| <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"> | |
| 86 |
|:---:|:---:|:---:|:---:|:---:|:---:| |
| 87 |
| Latest ✔ | Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | Latest ✔ | |
| 88 |
|
| 89 |
## License |
| 90 |
|
| 91 |
[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha |