-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevent_listeners.html
More file actions
100 lines (82 loc) · 2.52 KB
/
event_listeners.html
File metadata and controls
100 lines (82 loc) · 2.52 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!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">
<link rel="stylesheet" href="style.css">
<style>
body {
margin: 0;
min-height: 100vh;
}
body,
div {
display: flex;
justify-content: center;
align-items: center;
}
.grandparent {
width: 200px;
height: 200px;
background-color: red;
}
.parent {
width: 130px;
height: 130px;
background-color: blue;
}
.child {
width: 60px;
height: 60px;
background-color: green;
}
</style>
<title>Event Listenerst</title>
</head>
<body>
<h1>Event Listeners</h1>
<div class="grandparent">
<div class="parent">
<div class="child"></div>
</div>
</div>
<div class="notes">
<progress id="progress" max="100" value="75">75%</progress>
</div>
<script>
const grandparent = document.querySelector(".grandparent")
const parent = document.querySelector(".parent")
const child = document.querySelector(".child")
grandparent.addEventListener("click", e => {
console.log(e.target)
console.log("clicked grand parent")
})
parent.addEventListener("click", e => {
console.log(e.target)
console.log("clicked parent")
})
child.addEventListener("click", e => {
console.log(e.target)
console.log("clicked child")
})
document.addEventListener("click", e => {
console.log(e.target)
console.log("clciked somewhere in the document")
})
// ***********
//multiple events to a single element
"mouseover mouseout click".split(" ").forEach(function(event) {
document.querySelector("progress").addEventListener(event, changeBgColor, false)
})
function changeBgColor() {
let x = Math.floor(Math.random() * 256);
let y = Math.floor(Math.random() * 256);
let z = Math.floor(Math.random() * 256);
let bgColor = "rgb(" + x + "," + y + "," + z + ")";
console.log(bgColor);
document.body.style.background = bgColor;
}
</script>
</body>
</html>