getHTML()
The getHTML() method of the Element interface is used to serialize an element's DOM to an HTML string.
The method provides an options argument that enables the serialization of child nodes that are shadow roots. The options can be used to include nested shadow roots that have been set as serializable, and/or a specified array of ShadowRoot objects, which may be either open or closed.
Without arguments, child nodes that are shadow roots are not serialized, and this method behaves in the same way as reading the value of Element.innerHTML.
Note that some browsers serialize the < and > characters as < and > when they appear in attribute values (see Browser compatibility). This is to prevent a potential security vulnerability (mutation XSS) in which an attacker can craft input that bypasses a sanitization function, enabling a cross-site scripting (XSS) attack.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 125 | 125 | 128 | 18 | 125 | 18 | |
| The getHTML() method of the ShadowRoot interface is used to serialize a shadow root's DOM to an HTML string. | 125 | 125 | 128 | 18 | 125 | 18 |
Syntax
getHTML(options) Use cases
-
Using getHTML()
The getHTML() method of the Element interface is used to serialize an element's DOM to an HTML string.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.