-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-binding.js
More file actions
61 lines (52 loc) · 1.26 KB
/
event-binding.js
File metadata and controls
61 lines (52 loc) · 1.26 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
/* SAMPLE MARKUP
<ul id="fruit-list" class="fruit-list">
<li>
<button
class="c-btn c-btn--blue c-btn--sm"
type="button">Apple</button>
</li>
<li>
<button
class="c-btn c-btn--blue c-btn--sm"
type="button">Banana</button>
</li>
<li>
<button
class="c-btn c-btn--blue c-btn--sm"
type="button">Orange</button>
</li>
<li>
<button
class="c-btn c-btn--blue c-btn--sm"
type="button">Mango</button>
</li>
<li>
<button
class="c-btn c-btn--blue c-btn--sm"
type="button">Papaya</button>
</li>
</ul>
<br>
<button
class="alert"
href="#"
type="button">Click Me</button>
*/
// SOLUTION
document.addEventListener("DOMContentLoaded", () => {
const todos = document.querySelector('#fruit-list');
// This will bind a click event to all list
// items inside of the #todos ul element
todos.addEventListener('click', e => {
if (e.target && e.target.nodeName === 'BUTTON') {
alert(`You selected: ${e.target.innerHTML}`);
}
});
// This will bind a click event to all
// elments with the class name ".alert"
document.body.addEventListener('click', e => {
if (e.target && e.target.classList.contains('alert')) {
alert('You clikced an alert!');
}
});
});