Example 1 — Default Sanitize (MDN)
Inject HTML; <script> is removed automatically.
JavaScript
const target = document.getElementById("target");
const unsanitized = "abc 
Element.setHTML() parses and sanitizes a string of HTML, then inserts it as a subtree of the element (MDN). Prefer it over innerHTML for user-provided HTML when supported. It returns undefined.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Call el.setHTML. Start with this line.
element.setHTML(input) | Call | What it does |
|---|---|
element.setHTML(input) | Primary signature. |
el.setHTML(...) | Call with the arguments your case needs. |
Built into the browser. Call setHTML(...) on an Element — no import required.
element.setHTML(input) | Parameter | Description |
|---|---|
| input | Required. HTML string to sanitize and inject |
| options.sanitizer | Optional. Sanitizer, config, or "default" |
| Return Value | Description |
|---|---|
| * | Always undefined |
Inject HTML; <script> is removed automatically.
const target = document.getElementById("target");
const unsanitized = "abc