Hii @kartik,
It is possible to list all event listeners in JavaScript. You just have to hack the prototype's method of the HTML elements (before adding the listeners).
function reportIn(e){
var a = this.lastListenerInfo[this.lastListenerInfo.length-1];
console.log(a)
}
HTMLAnchorElement.prototype.realAddEventListener = HTMLAnchorElement.prototype.addEventListener;
HTMLAnchorElement.prototype.addEventListener = function(a,b,c){
this.realAddEventListener(a,reportIn,c);
this.realAddEventListener(a,b,c);
if(!this.lastListenerInfo){ this.lastListenerInfo = new Array()};
this.lastListenerInfo.push({a : a, b : b , c : c});
};
Now every anchor element (a) will have a lastListenerInfo property wich contains all of its listeners. And it even works for removing listeners with anonymous functions.
Another method
This solution will work only on Google Chrome or Chromium-based browsers.
We are going to use this website as an example.
Let’s say you wanted to find all the event listeners attached to the search icon DOM element.
![image](https://qphs.fs.quoracdn.net/main-qimg-96c1b88b5a252e000224426dce66ce92)
Right-click on the search icon button and choose “inspect” to open the Chrome developer tools.
![image](https://qphs.fs.quoracdn.net/main-qimg-da2ed3b168dd91831fa4a7846f926c67)
Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element.
![image](https://qphs.fs.quoracdn.net/main-qimg-86a41db5e4b39f889daf717b70bb44ad)
You can expand any event listener by clicking the right-pointing arrowhead.
When you expand an event listener and hover over the element, the “remove” button will appear. It can be used to delete the event listener.
![image](https://qphs.fs.quoracdn.net/main-qimg-7f7e955313ced308b6935d64b6c157d4)
Clicking on the filename main.min.js:6 takes you directly to the event listener callback function source by automatically switching you to the “sources” tab.
![image](https://qphs.fs.quoracdn.net/main-qimg-ba4cd7491ef3d3bd868c00e788f0da41)
As you can see in the screenshot below, you have been taken to the exact location of the event listener callback function.
![image](https://qphs.fs.quoracdn.net/main-qimg-d797a3d1f419f667c0fc39c2779142d6)
We can’t see the event listener code properly right now since the code is minified.
Click on the {} icon to format the code.
![image](https://qphs.fs.quoracdn.net/main-qimg-320504407d1d9829d64312a679cdc261)
Hope it helps!
In any case if you need to know more about Javascript code, then enroll with our Java certification course and learn from the top level instructors.