Element: getHTML() method
Baseline 2024Newly available
Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
ThegetHTML() method of theElement 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 asserializable, and/or a specified array ofShadowRoot 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 ofElement.innerHTML.
Note that some browsers serialize the< and> characters as< and> when they appear in attribute values (seeBrowser compatibility).This is to prevent a potential security vulnerability (mutation XSS) in which an attacker can craft input that bypasses asanitization function, enabling a cross-site scripting (XSS) attack.
In this article
Syntax
getHTML(options)Parameters
optionsOptionalAn options object with the following optional parameters:
serializableShadowRootsA boolean value that specifies whether to include
serializableshadow roots.The default value isfalse.shadowRootsAn array of
ShadowRootobjects to serialize.These are included regardless of whether they are marked asserializable, or if they are open or closed.The default value is an empty array.
Return value
A string that represents the HTML serialization of the element.
Exceptions
None.
Specifications
| Specification |
|---|
| HTML> # dom-element-gethtml> |