coderef

getHTML()Baseline · Newly available

The getHTML() method of Element and ShadowRoot objects serializes an element's DOM to an HTML string.

Works in the latest version of every major browser, but older devices may not support it yet. Baseline since Sep 16, 2024.

Browser support

DesktopChrome
1–124Not supported
125+Supportedsince May 14, 2024
Current: 155
Edge
12–124Not supported
125+Supportedsince May 17, 2024
Current: 154
Firefox
1–127Not supported
128+Supportedsince Jul 9, 2024
Current: 157
Safari
1–17.6Not supported
18+Supportedsince Sep 16, 2024
Current: 27
MobileChrome Android
18–124Not supported
125+Supportedsince May 14, 2024
Current: 155
Firefox Android
4–127Not supported
128+Supportedsince Jul 9, 2024
Current: 157
Safari iOS
1–17.6Not supported
18+Supportedsince Sep 16, 2024
Current: 27

How to use

Every current browser supports this, but it only became Baseline on Sep 16, 2024. Use it as a progressive enhancement, or keep a fallback if you support older devices.

Example

Full syntax and examples are on MDN: getHTML.

Feature detection

JS · check before using
if ("Element" in globalThis && "getHTML" in Element.prototype) {
  // Element.getHTML is available
}

Sub-features

Support for each part of the feature (2). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
Element.getHTML1251251281812512818
ShadowRoot.getHTML1251251281812512818