JavaScript Element getHTML() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, the getHTML() method serializes an element’s DOM to an HTML string (MDN). Without options it matches reading innerHTML — shadow roots are not included.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.getHTML. Start with this line.

JavaScript
el.getHTML(options?)
CallWhat it does
el.getHTML(options?)Primary signature.
el.getHTML(...)Call with the arguments your case needs.

Built into the browser. Call getHTML(...) on an Element — no import required.

Library declarations
JavaScript
el.getHTML(options?)

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*HTML string (never throws — MDN)

JavaScript Examples

Example 1 — Basic Serialize

Turn an element’s children into an HTML string.

JavaScript
const el = document.getElementById("box");

if (typeof el.getHTML === "function") {
  console.log(el.getHTML());
} else {
  console.log(el.innerHTML);
}
Try It Yourself

How It Works

  1. Feature-detect. Baseline 2024 — older engines may not have it.
  2. Call getHTML(). No options → light DOM only (MDN).
  3. Get a string. Ready to log, copy, or send.
  4. Fall back to innerHTML. Same markup for simple elements.

Example 2 — vs innerHTML

For light DOM, both APIs return the same serialization.

JavaScript
const el = document.getElementById("box");
const a = typeof el.getHTML === "function" ? el.getHTML() : null;
const b = el.innerHTML;

console.log(a);
console.log(b);
console.log(a === b);
Try It Yourself

How It Works

  1. Serialize both ways. Default getHTML() vs innerHTML.
  2. Compare strings. Equal when there is no shadow content (MDN).
  3. Difference is options. Only getHTML can include shadows.
  4. Prefer getHTML for new code when you may need shadow serialization later.

Example 3 — Serializable Shadow Root

Include a shadow tree created with serializable: true.

JavaScript
const host = document.getElementById("host");
const shadow = host.attachShadow({ mode: "open", serializable: true });
shadow.innerHTML = "

inside shadow

"; if (typeof host.getHTML === "function") { console.log(host.getHTML()); // no shadow console.log(host.getHTML({ serializableShadowRoots: true })); }
Try It Yourself

How It Works

  1. Attach a serializable shadow. attachShadow({ serializable: true }).
  2. Default getHTML skips it. Same as innerHTML (MDN).
  3. Opt in with options. serializableShadowRoots: true.
  4. Or pass shadowRoots. Include specific open/closed roots by reference.

Browser Support

Element.getHTML() is Baseline 2024 Newly available (MDN: across browsers since September 2024). Feature-detect and fall back to innerHTML when needed.

✓ Baseline 2024 · Newly available

getHTML()

Serialize an element’s DOM to an HTML string — with optional shadow root inclusion.

New Baseline 2024
Google Chrome Recent (check MDN)
Supported
Mozilla Firefox Recent (check MDN)
Supported
Apple Safari Recent (check MDN)
Supported
Microsoft Edge Chromium-based
Supported
Opera Chromium-based paths
Supported
Internet Explorer Not available
Unavailable
getHTML() Newly available

Bottom line: Use getHTML for modern apps that may need shadow serialization; keep an innerHTML fallback for older browsers.

Frequently Asked Questions

It serializes an element’s DOM subtree to an HTML string. Without options, it behaves like reading Element.innerHTML — shadow roots are not included (MDN).
No. MDN marks Element.getHTML() as Baseline 2024 Newly available (across browsers since September 2024). It is not Deprecated, Experimental, or Non-standard. Feature-detect on older browsers.
An optional object with serializableShadowRoots (boolean, default false) and shadowRoots (array of ShadowRoot objects, default []). These control whether and which shadow trees are serialized (MDN).
For plain elements without shadow DOM, getHTML() and innerHTML return the same markup. getHTML() can also include serializable or specified shadow roots — innerHTML never serializes shadow content.
When true, getHTML() includes child shadow roots that were created with serializable: true in attachShadow() (MDN).
No. MDN lists no exceptions for getHTML(). It always returns a string.

Next: hasAttribute()

Learn how to check whether an element has a named attribute.

hasAttribute tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful