-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOM.js
More file actions
60 lines (44 loc) · 1.54 KB
/
DOM.js
File metadata and controls
60 lines (44 loc) · 1.54 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
Array vs NodeList vs HTMLCollection
All three (Array, NodeList, HTMLCollection) hold REFERENCES to DOM elements.
Change the element → DOM changes everywhere.
Array
- Static snapshot
- Fully flexible: forEach, map, filter, etc.
- Not live
- Created manually (e.g. [...nodes])
NodeList
- Usually static (frozen view)
- Has forEach (no map/filter)
- Returned by querySelectorAll()
- Can include non-element nodes (text, comments)
HTMLCollection
- Live (auto-updates with DOM)
- No forEach / map
- Returned by getElementsBy*
- Elements only
childNodes → NodeList (includes text/comments)
children → HTMLCollection (elements only)
e.target vs e.currentTarget
e.target
- Element where the event ACTUALLY happened
- Can be a child element
- Changes depending on where you click
e.currentTarget
- Element that has the event listener
- Always the same
- Never a child
If listener is on parent and child is clicked:
e.target → child
e.currentTarget → parent
If element has no children:
e.target === e.currentTarget
// e.preventDefault() vs (default button type) === (input type = submit)
- <button type="submit"> inside a form triggers form submission by default.
- Default submission = browser tries to send request + reload page.
- To prevent reload in JS:
btn.addEventListener('click', (e) => {
e.preventDefault(); // stops default submit behavior
// run your custom JS here
});
- Alternative: use <button type="button"> to never submit form.
- TL;DR: submit button → reload; preventDefault() stops it; type="button" avoids it entirely.