textContent
The DOM property that reads or replaces all the text inside a node, treating anything you assign as plain text rather than markup.
Choosing innerHTML instead for untrusted text opens the page to cross-site scripting.
In more detail
Because textContent never parses HTML, assigning user data to it cannot inject scripts, unlike innerHTML. Use it whenever you only need to display text.
Related terms
- innerHTML — The DOM property that reads or replaces an element's contents as an HTML string, which the browser parses into nodes.
- cross-site scripting — An attack in which someone gets their own script to run inside your page, where it can read data, act as the user or steal tokens.
Taught in
- HTML Parsing, Tokenization And The Document Object Model (DOM) — Frontend Architecture & Web Platform
- DOM Manipulation: DocumentFragment And HTML Templates — Frontend Architecture & Web Platform
- Content Security Policy (CSP), Trusted Types And XSS Defenses — Frontend Architecture & Web Platform