JavaScript Element setHTML() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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.

Syntax

Call el.setHTML. Start with this line.

JavaScript
element.setHTML(input)
CallWhat 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.

Library declarations
JavaScript
element.setHTML(input)

Parameters

ParameterDescription
inputRequired. HTML string to sanitize and inject
options.sanitizerOptional. Sanitizer, config, or "default"

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Default Sanitize (MDN)

Inject HTML; <script> is removed automatically.

JavaScript
const target = document.getElementById("target");
const unsanitized = "abc