Search code examples
javascripthtmleventsdom-eventsaddeventlistener

How to get the function of a eventListener?


I can use getEventListeners(myElem) to get the list of events, but how can I get the function of these events?

Example:

var myDiv = document.querySelector("#myDiv");

var myFunction = (el) => {
  console.log(el);
}

myDiv.addEventListener("mousedown", myFunction);

//getEventListeners(myDiv)./* pathToFunction */.myFunction("test");
//expected log: "test" 
<div id="myDiv">This is my element, click it!</div>


Solution

  • Works only in Chrome:

    window.getEventListeners(document.querySelector("#myDiv")).mousedown[0].listener