Anonymous functions and removeEventListener?

0 like 0 dislike
5 views
Good day!
Given the following code:

function text(a) { alert(a); }


Let's say I need to pass parameters to the function that will handle the event, for this I use an anonymous function wrapper.

Code:

el.addEventListener('click', function() { text('it\\'s finnaly working!'); });


The question arises, how do I then remove event listener from this item if the two anonymous functions are different functions and this notation doesn't work:

Code:

el.removeEventListener('click', function() { text('it\\'s finnaly working!'); });
by | 5 views

1 Answer

0 like 0 dislike
It's simple: don't use anonymous functions.

var f1 = function() { text('it\\'s finnaly working!'); }; el.addEventListener('click', f1); // ... el.removeEventListener('click', f1);
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,698 users