# .getHTML()

> The `.getHTML()` JS method serializes the content of an element to an HTML string, optionally including shadow roots.

## How do I get the HTML of an element including shadow DOM?

The method that returns the inner HTML of an element as a string. Unlike `innerHTML`, it can include the contents of shadow roots in the result with the `serializableShadowRoots` option.

- Topic: Object on Page
- Type: method
- Browser support: Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 128+, Firefox Android 128+, Safari 18+, Safari iOS 18+

## Example

Wrong:

```js
const host = document.querySelector('my-card');
const html = host.innerHTML;
```

Right:

```js
const host = document.querySelector('my-card');
const html = host.getHTML({ serializableShadowRoots: true });
```

`innerHTML` skips shadow roots; `getHTML()` can serialize them as `<template shadowrootmode>` markup.

## Common mistakes

- Using `innerHTML` and wondering why the shadow DOM content is missing.

## Key points

- Shadow DOM is included with `getHTML({ serializableShadowRoots: true })`.
- Shadow roots that must be serialized are created with `serializable: true`.
- The result is not inserted back into the page without sanitizing if it can contain user content.

## Interview questions

### getHTML vs innerHTML?

Without options they return the same markup. `getHTML()` can also include serializable shadow roots, or specific ones passed in `shadowRoots`.

### How do I put getHTML output back into the page with its shadow DOM?

Use `setHTMLUnsafe()`, which parses declarative shadow DOM. Sanitize the string first if it can contain user content.

## Related cards

- [.innerHTML](https://wonderium.dev/js/inner-html.md)
- [.outerHTML](https://wonderium.dev/js/outer-html.md)
- [Shadow DOM](https://wonderium.dev/js/shadow-dom.md)
- [\[shadowrootmode\]](https://wonderium.dev/html/shadowrootmode.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/get-html
- Last updated: 2026-11-25
