Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element.innerHTML and Element.className

Use `innerHTML` to read or replace an element’s contents, and `className` to read or set its CSS classes.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

element.innerHTML;
element.innerHTML = markup;
element.className;
element.className = classNames;

Element.innerHTML/className is shorthand for two separate DOM instance properties, not methods. They belong to the web platform, not ECMAScript 2024: a JavaScript runtime without a DOM does not provide them.

Reading element.innerHTML serializes the current contents to a string, excluding the element’s own tags and any shadow roots. Setting it parses an HTML or XML fragment, depending on the document, and replaces the child nodes, not the element itself. Even identical markup creates new nodes: old references still point to the removed nodes, and their event listeners do not transfer. For a template, assignment replaces the child nodes of the DocumentFragment returned by content, not the fragment itself. Assigning null acts like assigning "". In an XML document, a malformed fragment throws a SyntaxError, and a serialization failure can throw an InvalidStateError; both are DOMException names.

On HTML elements, element.className reflects the class attribute as one string of whitespace-separated names. Assigning it replaces the entire attribute value without changing the child nodes. To change one class and keep the others, use classList.add(), classList.remove(), or classList.toggle(). On SVG elements, className can be an SVGAnimatedString; use getAttribute("class") or setAttribute("class", value) to read or write the whole attribute instead. getAttribute() returns null if the attribute is absent and "" if it is present but empty.

innerHTML does not sanitize input by default: never assign untrusted input directly, because it can lead to cross-site scripting (XSS). For plain text in an ordinary element, use textContent so the text is not parsed as HTML. If you need markup from an untrusted source, sanitize it first. Where Trusted Types is supported, a policy can return sanitized content as TrustedHTML; the type itself does not sanitize. If CSP enforces Trusted Types and there is no default policy, assigning a string throws a TypeError.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>innerHTML and className</title>
</head>
<body>
  <script>
    {
      const card = document.createElement("div");
      card.className = "card muted";
      card.innerHTML = "<strong>Ready</strong>";
      const original = card.firstElementChild;

      card.className = "card active";
      card.classList.add("highlight");
      console.log(card.className);
      console.log(card.firstElementChild === original);

      card.innerHTML = "<strong>Ready</strong>";
      console.log(card.firstElementChild === original);
      console.log(card.innerHTML);
    }
  </script>
</body>
</html>

Result

card active highlight
true
false
<strong>Ready</strong>

Save the complete example as an HTML file and open it in a browser without CSP enforcement of Trusted Types. Read the output in the browser console. The element stays detached. Changing its classes preserves the child node; assigning identical HTML replaces it.

Related lessons

  • JavaScript · Lesson 42 · Intermediate · Version: 3

Sources