Skip to the workspace
Pebellslearn by building
DOM APIs  ›  Lesson

Modifying the DOM

DOM APIs 15 min

javascript / Modifying the DOM

Explore each scene at your own pace.

01 · Modifying the DOM

Modifying the DOM

Modifying the DOM

DOM modification changes the browser’s document objects. textContent inserts text, while createElement and append build element structure.

Keep untrusted article text as text instead of interpreting it as HTML.

See the mechanism

Walk through the modelModifying the DOMAuthored snapshots of the example; use Run below to test the code.
itemdetached li
visible listempty
Allocation alone does not attach the node to the document.
1 of 3

Follow one example

html

createElement makes an li object that is initially detached from the page. Setting textContent fills its text. append attaches that object under the list, making it visible. Appending the same node elsewhere moves it; it does not clone it. Avoid placing untrusted content into innerHTML, which interprets markup and can create injection risks.

Predict, change and check

Predict: Will setting textContent to "Morning" create a bold element or display those characters?

Test your explanation: Try that text, then create an actual strong element separately if structure is intended. Remove append temporarily and explain why a created node need not be visible yet.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Render text as text

createElement and textContent construct nodes and plain text without interpreting an article title as markup. innerHTML parses markup and requires a separate trust and sanitization policy. Replacing nodes can discard focus and node-owned listeners, so keep model state outside disposable view nodes.

Batch a group of nodes when it clarifies the update, but verify an actual performance hypothesis before promising a speedup. Distinguish removing a node from releasing every reference to it: an array, closure or listener can still retain a detached node. Test the visible result and its ownership separately.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.