Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2017 15:40
Show Gist options
  • Star 83 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save dmnsgn/36b26dfcd7695d02de77f5342b0979c7 to your computer and use it in GitHub Desktop.
Save dmnsgn/36b26dfcd7695d02de77f5342b0979c7 to your computer and use it in GitHub Desktop.
List all event listeners in a document
const listeners = (function listAllEventListeners() {
let elements = [];
const allElements = document.querySelectorAll('*');
const types = [];
for (let ev in window) {
if (/^on/.test(ev)) types[types.length] = ev;
for (let i = 0; i < allElements.length; i++) {
const currentElement = allElements[i];
for (let j = 0; j < types.length; j++) {
if (typeof currentElement[types[j]] === 'function') {
"node": currentElement,
"listeners": [ {
"type": types[j],
"func": currentElement[types[j]].toString(),
return elements.filter(element => element.listeners.length)
Copy link

tkafka commented Mar 6, 2019

Here's a fork that lists events on document as well:

Copy link

Copy link

joeyhub commented Jan 14, 2020

I use this to remove and create a tree of event listeners for debugging:

    const all={};
    for(let tag of document.querySelectorAll('*')) {
        const events=getEventListeners(tag);
        if(Object.keys(events).length) {
            for(let [event,listeners] of Object.entries(events))
                for(let listener of listeners)tag.removeEventListener(listener.type,listener.listener,listener.useCapture);
            const path=(node=>{
                const parts=[];
                while(node.parentNode!==null) {
                return parts.reverse()
            let node=all;
            for(let part of path) {
                if(!(part in node))node[part]={};
            node[name]=[tag, events]
    return all

It was originally a one liner hence the weird formatting. I'm not sure getEventListeners is standard JS but it's in the Chrome console.

Copy link

@joeyhub No, it is not a standard api in js. You can only use getEventListeners in dev tool of Chrome (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment