-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-prevent-default.html
40 lines (39 loc) · 1.22 KB
/
event-prevent-default.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!doctype html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>preventDefault() vs stopPropagation()</title>
<style>
input {
margin-top: 10px;
margin-bottom: 10px;
min-width: 200px;
}
</style>
</head>
<body>
<h1>preventDefault() vs stopPropagation()</h1>
<div>
<input id="text1" type="text" placeholder="this text box has stopPropagation()" />
</div>
<div>
<input id="text2" type="text" placeholder="this text box has preventDefault()" />
</div>
<script>
var element1 = document.getElementById('text1');
element1.addEventListener('keydown', function(event) {
if (event.keyCode === 27) {
element1.value = '';
event.stopPropagation(); // stops the event from bubbling up
}
});
var element2 = document.getElementById('text2');
element2.addEventListener('keydown', function(event) {
if (event.keyCode === 27) {
element2.value = '';
event.preventDefault(); // prevents the default action the browser makes
}
});
</script>
</body>
</html>