0

I need to pass a function argument into another function, how would you achieve that? ex:

function one(nextFunction) {
    nextFunction()
}

function listener(myFunction) {
document.getElementById("button").addEventListener("click", one(myFunction);
}

listener(theFunction);


function theFunction() {

alert("hello");
}
Ben
  • 3
  • 5
  • @CertainPerformance this doesn't answer my question... – Ben May 21 '20 at 17:05
  • It should. You need to do exactly what's described in the canonical Q/A there, and your code will work – CertainPerformance May 21 '20 at 17:06
  • I want the function in the event listener "one" to inherit the argument of the listener function. – Ben May 21 '20 at 17:08
  • @CertainPerformance I'm sorry but I can't find what I'm looking for... Could you reopen the thread ? – Ben May 21 '20 at 17:13
  • It is a duplicate of the canonical Q/A. You need to pass a function to `addEventListener` which, when called, invokes `one` with the argument you want, as shown in numerous places in the linked Q/A. – CertainPerformance May 21 '20 at 17:14
  • @CertainPerformance Yes but I'm using removeEventListener later on in the code and can't have an anonymous function in my listener. – Ben May 21 '20 at 17:16
  • 1
    Then assign the function to a variable so you can remove it later, before passing to `addEventListener` – CertainPerformance May 21 '20 at 17:16
  • @CertainPerformance Maybe I'm just dumb, sorry to spam in here but just don't understand. Could you translate my code here into an example please ? – Ben May 21 '20 at 17:18

0 Answers0