Study notes · 12% of the exam
DOM and Events
Select elements, listen for events, and use bubbling to handle many elements with one listener.
Key points
- 1
querySelectorandquerySelectorAllaccept any CSS selector. - 2
Events bubble from the target up through its ancestors, which enables event delegation with
event.target.closest(...). - 3
preventDefault()cancels the browser's default action;stopPropagation()stops bubbling. They are independent. - 4
DOMContentLoadedfires when the DOM is ready;loadwaits for images and other resources too.
Common traps
The
inputevent fires on every keystroke;changefires when the value is committed.
Read the source
Test yourself on DOM and Events
Ten questions, with the answer and explanation after each one.