EN
JavaScript - remove all events from DOM element with VanillaJS
4 points
In this article, we would like to show you how to remove events using VanillaJS.
xxxxxxxxxx
1
// 1. Removing all events by replacing element examples
2
3
// 1.1. Keeping all function references example
4
// this approach requires to write some logic what has been presented in below article
5
6
// 1.2. Cloning existing element and replacing it example
7
var clone = element.cloneNode(true);
8
parent.replaceChild(clone, element);
9
10
// 2. jQuery examples
11
// https://dirask.com/q/jquery-how-to-remove-event-zjMozD
There is no way to remove all events from the element. There are two solutions:
- keeping all function references and removing them with
removeEventListener
method, - cloning existing element and replacing it - clone operation does not copy events added with
addEventListener
method.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<script>
5
6
function addEvent(element, name, handler) {
7
var events = element.$EVENTS;
8
9
if(events == null)
10
events = element.$EVENTS = { };
11
12
var handlers = events[name];
13
14
if(handlers == null)
15
handlers = events[name] = [ ];
16
17
var index = handlers.indexOf(handler);
18
19
if(index == -1) {
20
handlers.push(handler);
21
element.addEventListener(name, handler);
22
}
23
}
24
25
function removeEvent(element, name, handler) {
26
var events = element.$EVENTS;
27
28
if(events == null)
29
return;
30
31
var handlers = events[name];
32
33
if(handlers == null)
34
return;
35
36
var index = handlers.indexOf(handler);
37
38
if(index > -1) {
39
handlers.splice(index, 1);
40
element.removeEventListener(name, handler);
41
}
42
}
43
44
function clearEvents(element, name) {
45
var events = element.$EVENTS;
46
47
if(events) {
48
function removeHandlers(handlers, name) {
49
for(var i = 0; i < handlers.length; ++i) {
50
element.removeEventListener(name, handlers[i]);
51
}
52
}
53
54
if(name) {
55
var handlers = events[name];
56
57
if(handlers) {
58
removeHandlers(handlers, name);
59
}
60
} else {
61
for(var key in events) {
62
removeHandlers(events[key], key);
63
}
64
65
delete element.$EVENTS;
66
}
67
}
68
}
69
70
</script>
71
</head>
72
<body>
73
<button id="my-button">
74
Click button...
75
</button>
76
<br /><br />
77
<button onclick="removeEvents();">
78
Remove all events...
79
</button>
80
<script>
81
82
var button = document.getElementById('my-button');
83
84
function onClick() {
85
console.log('Event name: click');
86
}
87
88
function onMouseover() {
89
console.log('Event name: mouseover');
90
}
91
92
function onMouseout() {
93
console.log('Event name: mouseout');
94
}
95
96
addEvent(button, 'click', onClick);
97
addEvent(button, 'mouseover', onMouseover);
98
addEvent(button, 'mouseout', onMouseout);
99
100
function removeEvents() {
101
clearEvents(button);
102
103
/*
104
clearEvents(button, 'click');
105
clearEvents(button, 'mouseover');
106
clearEvents(button, 'mouseout');
107
*/
108
}
109
110
</script>
111
</body>
112
</html>
Note: the biggest disadvantage of this approach is necessary to keep all references.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<script>
5
6
function clearEvents(element) {
7
var parent = element.parentNode;
8
9
if (parent) {
10
var clone = element.cloneNode(true);
11
12
parent.replaceChild(clone, element);
13
}
14
}
15
16
</script>
17
</head>
18
<body>
19
<button id="my-button" onclick="console.log('This kind of events are cloned.');">
20
Click button...
21
</button>
22
<br /><br />
23
<button onclick="removeEvents();">
24
Remove click event...
25
</button>
26
<script>
27
28
var button = document.getElementById('my-button');
29
30
function onClick() {
31
console.log('Event name: click');
32
}
33
34
function onMouseover() {
35
console.log('Event name: mouseover');
36
}
37
38
function onMouseout() {
39
console.log('Event name: mouseout');
40
}
41
42
button.addEventListener('click', onClick);
43
button.addEventListener('mouseover', onMouseover);
44
button.addEventListener('mouseout', onMouseout);
45
46
function removeEvents() {
47
clearEvents(button);
48
}
49
50
</script>
51
</body>
52
</html>
Note: this approach clones all event attributes / properties too (e.g.
onclick
). It means they should be removed manually.
How to remove events with jQuery has been described in this article.