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
detached li↓emptyFollow one example
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.