The DOM is the live tree of nodes. Select elements with document.querySelector and querySelectorAll.
Selectors
#id— unique id.class— class namebutton[type="submit"]— attributearticle > p— descendant
Matches CSS selector syntax—keep selectors specific but not brittle.
Live vs static NodeLists
querySelectorAll returns static collection; older APIs may be live and surprise you on DOM changes.
Important interview questions and answers
- Q: querySelector vs getElementById?
A: querySelector uses CSS selectors; getElementById is faster for ids only. - Q: null if missing?
A: Always check before calling methods on result.
Self-check
- Why check for null after query?
- Name two selector types.
Tip: Re-run the playground code for dom-selection and tweak one line before the MCQs.
Interview prep
- querySelector?
First match for CSS selector string.