Skip to content

Chapter 12 of 33

DOM Manipulation

Select, create, update, batch, and safely remove document nodes and attributes.

42 minutes 10 quick checksBy Subha Prasad
Lesson 12 of 33Course navigation

Lesson content

Read, practise, then check your understanding

The DOM models a document as nodes and elements. Select narrowly, retain useful references, and update semantic properties.

Safe construction

const list = document.querySelector("[data-lessons]");
const fragment = document.createDocumentFragment();

for (const title of titles) {
  const item = document.createElement("li");
  item.textContent = title;
  item.classList.add("lesson");
  fragment.append(item);
}
list.append(fragment);

Use textContent for untrusted text; innerHTML parses markup and can introduce XSS. dataset, attributes, properties, classList, closest, and matches support interaction. Batch changes and avoid layout read/write thrashing. Preserve focus, labels, keyboard behavior, and ARIA semantics when modifying interfaces.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes DOM?
Which JavaScript term matches this description: An object model representing an HTML/XML document tree.
Which statement best describes querySelector?
Which JavaScript term matches this description: A method returning the first element matching a CSS selector.
Which statement best describes textContent?
Which JavaScript term matches this description: A safe text property that does not parse HTML markup.
Which statement best describes classList?
Which JavaScript term matches this description: An API for adding, removing, toggling, and testing CSS classes.
Which statement best describes DocumentFragment?
Which JavaScript term matches this description: An off-document container used to batch node insertion.

0 of 10 checks passed

Your progress is saved on this device.