-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path11 - DOM Events.html
84 lines (68 loc) · 2.1 KB
/
11 - DOM Events.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Getting Started with JavaScript</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css">
<style>
body {
padding: 30px;
}
</style>
</head>
<body>
<!-- OPTION 1 -->
<div class="jumbotron text-center">
<button class="btn btn-danger" onclick="goCoocoo()">
Go Coocoo
</button>
</div>
<!-- OPTION 2: use events directly on the element in js -->
<div class="jumbotron text-center">
<button class="btn btn-danger coffee-btn">
Get Coffee
</button>
<button class="btn btn-danger coffee-btn">
Get Coffee
</button>
<button class="btn btn-danger coffee-btn">
Get Coffee
</button>
</div>
<!-- OPTION 3: using addEventListener -->
<div class="jumbotron text-center">
<button class="btn btn-primary lala-btn">
Not Listening
</button>
</div>
<!-- 🔥🔥🔥🔥 start javascript 🔥🔥🔥🔥 -->
<script>
// 1 grab elements from the dom
// 2 attach event listeners
function goCoocoo() {
document.body.style.backgroundColor = '#C00C00';
}
// OPTION 2 ========================================
const coffeeButtons = document.querySelectorAll('.coffee-btn');
// coffeeButton.onclick = function() {
// document.body.style.backgroundColor = '#C0FFEE';
// }
// coffeeButton.onmouseenter = goCoocoo;
function getCoffee() {
document.body.style.backgroundColor = '#C0FFEE';
}
coffeeButtons.forEach(function (button) {
button.onclick = getCoffee;
});
// OPTION 3 =======================================
const lalaButton = document.querySelector('.lala-btn');
function notListening() {
document.body.style.backgroundColor = '#1A1A1A';
}
lalaButton.addEventListener('click', notListening);
lalaButton.addEventListener('mouseenter', goCoocoo);
const buttons = document.querySelectorAll('button');
buttons.forEach(button => button.addEventListener('mouseleave', notListening));
</script>
</body>
</html>