CKTIS / JavaScript
Element.innerHTML, tagName, and namespaceURI
Read and replace an element’s markup, and distinguish elements by local name and namespace when HTML and SVG share the same DOM tree.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
element.innerHTML
element.innerHTML = markup
element.tagName
element.namespaceURIElement.innerHTML/tagName/namespaceURI covers three DOM properties: one for content and two for identifying the element. Reading innerHTML returns a string serialized from the element’s current contents, without its own tags or any shadow roots. Assigning a string or TrustedHTML parses a fragment in the element’s context and replaces its child nodes, not the element itself. References to old nodes still point to those objects, not to the replacements. For an HTML template element, the getter and setter work on its content. Setting null is equivalent to setting an empty string.
An HTML document uses the HTML parser, which can recover from malformed markup. An XML document, including XHTML served as XML, uses the XML parser; a fragment that is not well-formed throws a DOMException named SyntaxError.
tagName is the read-only qualified name, including a prefix if there is one. It is ASCII-uppercased only for elements in the HTML namespace whose document is HTML. namespaceURI is also read-only: it returns the namespace URI set when the element was created, or null for no namespace. Moving the element or editing an xmlns attribute does not change it. In a mixed-namespace tree, compare namespaceURI and localName (the name without its prefix); use document.createElementNS() to choose a namespace explicitly when creating an element.
Do not assign untrusted markup to innerHTML: it does not sanitize by default and can enable XSS, for example through event-handler attributes. For plain text, use textContent. When CSP enforces Trusted Types, assigning a plain string without a default policy throws TypeError. A TrustedHTML value is only as safe as the policy that produced it. These are web-platform APIs, not ECMAScript 2024 features; do not assume they exist in runtimes without a DOM.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML and SVG element names</title>
</head>
<body>
<script>
const container = document.createElement('div');
const previous = document.createElement('span');
container.appendChild(previous);
container.innerHTML = '<a>HTML</a><svg><a>SVG</a></svg>';
const htmlLink = container.firstElementChild;
const svgLink = container.lastElementChild.firstElementChild;
console.log(container.innerHTML);
for (const link of [htmlLink, svgLink]) {
console.log(`${link.tagName} | ${link.localName} | ${link.namespaceURI}`);
}
console.log(previous.parentNode === null);
</script>
</body>
</html>Result
<a>HTML</a><svg><a>SVG</a></svg> A | a | http://www.w3.org/1999/xhtml a | a | http://www.w3.org/2000/svg true
Save the example as an `.html` file and open it directly in a browser. Read the output in the developer console. Both elements have `localName` equal to `a`, but their `tagName` and `namespaceURI` differ. The final `true` means the old child no longer has a parent.
Related lessons
- JavaScript · Lesson 16 · Advanced · Version: 4