0

Using addEventLIstener and removeEventListener, is there a way in JavaScript to pass a value to the handler function, via an anonymous function, and still remove the event later in the code?

For example, I was using element.addEventLister( 'mousedown', func, false ) and then later removing the same; but if want element.addEventListener( 'mousedown', function() { func(x); }, false ), the event listener can't be removed for an anonymous function, or at least that is what I read on MDN and I haven't been able remove it.

I wanted to make a closure on func such that the event would pass the value of x at the time the event listener was added/registered rather than the value when the event is triggered. That worked, but the listener couldn't be removed later.

Thank you.

After using bind successfully for some cases, I don't understand how to get the event object when using bind and still be able to remove the event listener afterward.

For example, the following code makes a new function f that is like func with a closure on x set to value 2. The only purpose in doing this is to make the listener removable and still pass arguments to the handler function. But how can the focus event object be available to f or func and the listener still be removed?

let x, f;
function func(a) { /* code...*/ }
x = 2;
f = func.bind(null, x);
element.addEventListener( 'focus', f, false );
x = 5;
// and later element.removeEventListener( 'focus', f, false );

In the case where the event listener won't be removed, I have been using something like element.addEventListener( 'focus', function(evt) { func(evt, a); }, false) and declaring func as func(evt, a); but that listener can't be removed directly because the handler is an anonymous function.

I'm assuming there must be a simple way to do this but I'm not understanding it at this point.

Thank you.

Gary
  • 2,393
  • 12
  • 31
  • 1
    Give a name to your wrapping function? It doesn't need to be anonymous. Also you might be interested in [Function#bind()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind). – Kaiido Sep 28 '18 at 02:20
  • @Kaido Thank you. I read the first two links earlier before asking the question; and maybe I'm missing something but I don't see how they provide a solution to my question. However, the third link which uses bind I have not seen before and seems to provide a way to do what I've been trying. I'll give it a try. – Gary Sep 28 '18 at 02:54
  • Well they tell you to give a name to your wrapping function or to store it somewhere. That's all you really need. – Kaiido Sep 28 '18 at 03:06
  • @Kaido Perhaps, but I don't see how; at least not in my specific case. The bind method worked well and provided a closure also. Thanks again. – Gary Sep 28 '18 at 03:43
  • @Kaido If you have a moment would you mind explaining what I am not understanding regarding using bind and passing the event object to the event handler? I added an example to the question above. Thank you for your help. – Gary Sep 29 '18 at 03:37
  • 1
    Normal arguments are passed after the values you did bound your function with. e.g `function func(a, b, c){console.log(a, b, c);} var bound = func.bind(null, 'j'); bound('k', 'l');` will log `"j", "k", "l"`, because we bound `"j"` as being always the first argument of `bound`. So in your case, the Event object will be the second argument of your bound function. – Kaiido Sep 29 '18 at 03:50
  • @Kaido Thank you. Following your information, the only change I made in the above example was to declare func as func(a, evt) and the addEventListener automatically bound the event object as the last argument of f. Would you mind teling me how you knew that? I have a 1,000 plus page JS book in front of me and read several documents on MDN and I didn't come across any information that even implied this. I'd just like to know where to look. Thank you. – Gary Sep 29 '18 at 04:10

0 Answers0