Anonymous functions and removeEventListener?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
30 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 | 30 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,187 answers
0 comments
40,796 users