I have tried many things, but none of them work. I wonder if it’s impossible? I know the ‘normal’ way with ‘bind’, but the arrow functions are much more readable and I prefer to use them.
To better understand my question I made this sample code that illustrates the problem as fully as possible.
JavaScript
x
42
42
1
class MyClass_XY {
2
3
constructor(zID) {
4
let ref = document.getElementById(zID);
5
this.name = zID;
6
this.Info = ref.querySelector('span');
7
this._Bt_Plus = ref.querySelector('.plus');
8
this._bt_Stop = ref.querySelector('.stop');
9
10
this.Nbre = 0;
11
// this.stop = false; // I don't whant this, because this is a small sample code of something more complex
12
13
this._Bt_Plus.onclick = e => this._f_Bt_Plus_click(e);
14
this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
15
16
/*
17
this.fct_Ref = null;
18
this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
19
*/
20
}
21
22
_f_Bt_Plus_click(e) {
23
e.stopPropagation();
24
console.log(e.target.innerText);
25
this.Nbre++,
26
this.Info.innerText = this.Nbre.toString();
27
}
28
29
_f_Bt_Stop_click(e) {
30
e.stopPropagation();
31
32
// this._Bt_Plus.removeEventListener('click', this.fct_Ref , false ); // is OK, how to deal the other ?
33
34
this._Bt_Plus.removeEventListener("click", this._f_Bt_Plus_click, true); // didn't work :/
35
36
console.log(this.name, '_Bt_Plus remove onclick ');
37
}
38
}
39
40
var
41
Ananas = new MyClass_XY('Pineapple'), // I am a frog
42
Bananes = new MyClass_XY('Bananas');
JavaScript
1
9
1
<p id='Pineapple'> pineapple <span>0</span>
2
<button class="plus">+1 pineapple</button>
3
<button class="stop">stop</button>
4
</p>
5
6
<p id='Bananas'> Bananas <span>0</span>
7
<button class="plus">+1 Bananas</button>
8
<button class="stop">stop</button>
9
</p>
Advertisement
Answer
Because you didn’t add the listener using addEventListener
, removeEventListener
won’t work – to remove a listener attached by assigning to onclick
, simply assign null
to
the onclick
property again:
JavaScript
1
2
1
this._Bt_Plus.onclick = null;
2
JavaScript
1
42
42
1
class MyClass_XY {
2
3
constructor(zID) {
4
let ref = document.getElementById(zID);
5
this.name = zID;
6
this.Info = ref.querySelector('span');
7
this._Bt_Plus = ref.querySelector('.plus');
8
this._bt_Stop = ref.querySelector('.stop');
9
10
this.Nbre = 0;
11
// this.stop = false; // I don't whant this, because this is a small sample code of something more complex
12
13
this._Bt_Plus.onclick = e => this._f_Bt_Plus_click(e);
14
this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
15
16
/*
17
this.fct_Ref = null;
18
this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
19
*/
20
}
21
22
_f_Bt_Plus_click(e) {
23
e.stopPropagation();
24
console.log(e.target.innerText);
25
this.Nbre++,
26
this.Info.innerText = this.Nbre.toString();
27
}
28
29
_f_Bt_Stop_click(e) {
30
e.stopPropagation();
31
32
// this._Bt_Plus.removeEventListener('click', this.fct_Ref , false ); // is OK, how to deal the other ?
33
34
this._Bt_Plus.onclick = null;
35
36
console.log(this.name, '_Bt_Plus remove onclick ');
37
}
38
}
39
40
var
41
Ananas = new MyClass_XY('Pineapple'), // I am a frog
42
Bananes = new MyClass_XY('Bananas');
JavaScript
1
9
1
<p id='Pineapple'> pineapple <span>0</span>
2
<button class="plus">+1 pineapple</button>
3
<button class="stop">stop</button>
4
</p>
5
6
<p id='Bananas'> Bananas <span>0</span>
7
<button class="plus">+1 Bananas</button>
8
<button class="stop">stop</button>
9
</p>
If you did use addEventListener
, then to use removeEventListener
later, you would have to have a reference to the same function you passed in to addEventListener
originally, such as with
JavaScript
1
3
1
this.plusHandler = e => this._f_Bt_Plus_click(e);
2
this._Bt_Plus.addEventListener('click', this.plusHandler);
3
and then
JavaScript
1
2
1
this._Bt_Plus.removeEventListener("click", this.plusHandler);
2
JavaScript
1
43
43
1
class MyClass_XY {
2
3
constructor(zID) {
4
let ref = document.getElementById(zID);
5
this.name = zID;
6
this.Info = ref.querySelector('span');
7
this._Bt_Plus = ref.querySelector('.plus');
8
this._bt_Stop = ref.querySelector('.stop');
9
10
this.Nbre = 0;
11
12
this.plusHandler = e => this._f_Bt_Plus_click(e);
13
this._Bt_Plus.addEventListener('click', this.plusHandler);
14
15
this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
16
17
/*
18
this.fct_Ref = null;
19
this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
20
*/
21
}
22
23
_f_Bt_Plus_click(e) {
24
e.stopPropagation();
25
console.log(e.target.innerText);
26
this.Nbre++,
27
this.Info.innerText = this.Nbre.toString();
28
}
29
30
_f_Bt_Stop_click(e) {
31
e.stopPropagation();
32
33
// this._Bt_Plus.removeEventListener('click', this.fct_Ref , false ); // is OK, how to deal the other ?
34
35
this._Bt_Plus.removeEventListener("click", this.plusHandler);
36
37
console.log(this.name, '_Bt_Plus remove onclick ');
38
}
39
}
40
41
var
42
Ananas = new MyClass_XY('Pineapple'), // I am a frog
43
Bananes = new MyClass_XY('Bananas');
JavaScript
1
9
1
<p id='Pineapple'> pineapple <span>0</span>
2
<button class="plus">+1 pineapple</button>
3
<button class="stop">stop</button>
4
</p>
5
6
<p id='Bananas'> Bananas <span>0</span>
7
<button class="plus">+1 Bananas</button>
8
<button class="stop">stop</button>
9
</p>