Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/colxi/getEventListeners
Returns an object containing all the event listeners of a DOM Node
https://github.com/colxi/getEventListeners
Last synced: 15 days ago
JSON representation
Returns an object containing all the event listeners of a DOM Node
- Host: GitHub
- URL: https://github.com/colxi/getEventListeners
- Owner: colxi
- License: mit
- Created: 2018-09-04T06:27:19.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-05T15:11:59.000Z (over 2 years ago)
- Last Synced: 2024-09-21T12:55:51.534Z (about 2 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 76
- Watchers: 3
- Forks: 16
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Element.prototype.getEventListeners()
Returns the event listeners registered on the specified object. The return value is an object that contains an array for each registered event type (click or keydown, for example). The members of each array are objects that describe the listener registered for each type.
A simple usage example :
```javascript
let myEl = document.getElementById('someElementId');
// add some event listeners to the Element
myEl.addEventListener('click', e=> console.log('click!') );
myEl.addEventListener('click', e=> console.log('click 2!') );
myEl.addEventListener('mouseover', e=> console.log('mouse over!') );// retrieve the listeners
console.log( myEl.getEventListeners() );
/*
{
click : [
{ listener: ƒ, useCapture: false, type:"click"},
{ listener: ƒ, useCapture: false, type:"click"}
],
mouseover : [
{ listener: ƒ, useCapture: false, type:"mouseover"}
]
}
*/
```Retrieve only the click-event listeners :
```javascript
console.log( myEl.getEventListeners('click') );
/*
[
{ listener: ƒ, useCapture: false, type:"click"},
{ listener: ƒ, useCapture: false, type:"click"}
]
*/
```
## Package distribution :
You can include this library using the CDN ...```
```
Package can also be installed via:
```
$ npm install geteventlisteners --save
```and is also available in Github :
```
https://github.com/colxi/getEventListeners
```## Limitations
To be able to track the event listeners assignements, this module overwrites `Element.prototype.addEventListener` and `Element.prototype.removeEventListener`, with custom functions that mantain an updated list of listeners.
Any event listener declared before this module is imported will be missed.> **In order to be able to track all the listeners, this module must be imported before any event listener is declared.**
**Only event listeners declared using `Element.addEventListener()`, are going to be tracked**. For example, the following event listeners declarations, will not be tracked :
```html
```
and...
```javascript
myElement.onclick= function(){ /*your code*/ }
```