-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventBubbling.html
47 lines (37 loc) · 1.48 KB
/
eventBubbling.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
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Kauua is bird</h1>
<div id="parent">outside parent
<button id="child">inside child</button>
</div>
<script type="text/javascript">
// event bubbling
document.getElementById("parent")
.addEventListener("click", function(event) {
console.log("parent is clicked", event);
}); //3rd argument, by default it is false i.e. event bubbling
document.getElementById("child")
.addEventListener("click", function(event) {
// event.stopPropagation() // to prevent event bubbling
console.log("child is clicked", event);
});
// // event capturing
// document.getElementById("parent")
// .addEventListener("click", function(event) {
// // event.stopPropagation() // to prevent event bubbling
// console.log("parent is clicked", event);
// }, true); //this third argument as true, enables event capturing // by default it is false i.e. event bubbling
// document.getElementById("child")
// .addEventListener("click", function(event) {
// console.log("child is clicked", event);
// });
</script>
</body>
</html>