The DOM — Selecting and Changing the Page — Concept Check
Questions on The DOM — Selecting and Changing the Page. The DOM is a tree of objects representing the page. You will mostly let React manage it, but understanding it is what lets you debug React and handle the cases React does not cover.
Questions in this quiz
8 questions on The DOM — Selecting and Changing the Page — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a list of items and want to update their heights based on their current heights. Which approach avoids layout thrashing?
- ABatch all reads first, then perform all writes
- BUse innerHTML to update the heights
- CUse getElementsByClassName for a live collection
- DRead and write in a loop for each item
2.A developer uses `document.querySelectorAll('.item')` to select elements. They notice changes to the DOM are not reflected in the NodeList. Why?
- AquerySelectorAll returns a live collection
- BThe NodeList is not iterable
- CgetElementsByClassName should be used instead
- DquerySelectorAll returns a static NodeList
3.You need to change the text of an element securely. Which method should you use to avoid XSS vulnerabilities?
- Ael.innerHTML = sanitize(userInput)
- Bel.innerHTML = userInput
- Cel.textContent = userInput
- Del.outerHTML = userInput
4.A developer is iterating over elements using `getElementsByClassName` and appending new elements inside the loop. What issue might they encounter?
- AInfinite loop due to live collection
- BStatic NodeList causing no updates
- CElements not being appended
- DSyntax error in the loop
5.You want to toggle a class on an element based on a condition. Which method allows you to do this directly?
- Ael.classList.add('open', isOpen)
- Bel.classList.contains('open', isOpen)
- Cel.classList.toggle('open', isOpen)
- Del.classList.replace('open', isOpen)
6.A developer tries to access a custom data attribute using `el.dataset.problemId`. What will they get if the attribute is set to 42?
- A42 as a number
- Bundefined if not set
- Cnull if not set
- D'42' as a string
7.When measuring an element's dimensions, which property read can cause layout thrashing if not batched?
- Ael.classList.contains
- Bel.dataset.length
- Cel.style.height
- Del.offsetHeight
8.You need to select the first element with the class 'card'. Which method should you use?
- Adocument.querySelector('.card')
- Bdocument.querySelectorAll('.card')[0]
- Cdocument.getElementById('card')
- Ddocument.getElementsByClassName('card')[0]