0

If I pass the main code to a different function, how do I pass the e variable? Is there any clean way I can add a fake event to the addEventListener parameters just for the purpose of removing it or something?

containers.forEach(container => {
  container.addEventListener('dragover', e => {
    //main code, uses e
  })
})

function f() {
  containers.forEach(container => {
    container.removeEventListener('dragover', ? ? ? )
  })
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Just define a named function instead of using an anonymous function. Named functions can take an `e` parameter. – Barmar Feb 03 '22 at 01:01
  • since with every iteration each container gets subscribed its own anonymous handler function, but each single handler is implemented alike, one just has to implement this handler once (e.g. as [*function statement*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function) or as named [*function expression*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function)). Then one can add and remover listeners as one likes since one has to handle just a single well known handler reference. – Peter Seliger Feb 03 '22 at 01:03

0 Answers0