Element.getHTML() is an instance method that serializes an element’s DOM to an HTML string. Learn basic serialization, how it compares to innerHTML, shadow DOM options (serializableShadowRoots and shadowRoots), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
HTML string
03
Args
options (opt)
04
Shadow
Configurable
05
Default
Like innerHTML
06
Status
Baseline 2024
Fundamentals
Introduction
When you need to export or copy the HTML inside an element, you might reach for innerHTML. getHTML() is the modern method that does the same job — and adds optional control over shadow DOM serialization.
Without any arguments, MDN states that getHTML() behaves like reading innerHTML: child nodes that are shadow roots are not serialized. Pass an options object when you need shadow content in the output string.
💡
Beginner tip
MDN notes that some browsers escape < and > in attribute values during serialization to reduce mutation XSS (mXSS) risk. Always treat serialized HTML as untrusted if it came from user input.
Element.getHTML() is Baseline 2024 (MDN: newly available across browsers since September 2024). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline 2024
Element.getHTML()
Serialize element DOM to HTML with optional shadow root control — newer than innerHTML reads alone.
Baseline2024 feature
Google ChromeSupported · recent versions
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · recent versions
Yes
Apple SafariSupported · recent versions
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot supported
No
getHTML()Growing
Bottom line: Feature-detect getHTML before use in production. Fallback to innerHTML for plain reads when shadow serialization is not needed.
Wrap Up
Conclusion
Element.getHTML() serializes an element’s descendants to an HTML string. With no options it matches innerHTML; with shadow options it is the modern way to export markup from web components.
Use for read-only serialization (not writing HTML)
Pass shadow options only when you need shadow content
Set serializable: true in attachShadow when exporting components
Sanitize serialized strings from untrusted sources
❌ Don’t
Assume shadow DOM is included by default
Re-inject serialized HTML without sanitization
Use instead of textContent for plain text
Expect IE or very old browsers to support it
Confuse with outerHTML (includes the element itself)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getHTML()
Serialize DOM to HTML strings.
5
Core concepts
📝01
Returns
HTML string
API
📄02
Default
innerHTML
Same
✍️03
Shadow
options
Opt-in
✅04
Baseline
2024
Status
⚡05
Safety
sanitize
mXSS
❓ 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.
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() adds options to 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.
Did you know?
MDN: without arguments, getHTML() behaves the same as innerHTML. The main reason to use the method is optional shadow DOM serialization via serializableShadowRoots or shadowRoots.