Skip to main content

ShadowRoot

The ShadowRoot interface of the Shadow DOM API is the root node of a DOM subtree that is rendered separately from a document's main DOM tree.

MDN Reference

Extends

Accessors

textContent

Get Signature

get textContent(): string;

MDN Reference

Returns

string

Set Signature

set textContent(value): void;

The textContent property of the Node interface represents the text content of the node and its descendants.

MDN Reference

Parameters
ParameterType
valuestring | null
Returns

void

The textContent property of the Node interface represents the text content of the node and its descendants.

MDN Reference

Inherited from

DocumentFragment.textContent

Methods

addEventListener()

Call Signature

addEventListener<K>(
type,
listener,
options?
): void;

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

Type Parameters
Type Parameter
K extends "slotchange"
Parameters
ParameterType
typeK
listener(this, ev) => any
options?boolean | AddEventListenerOptions
Returns

void

Overrides

DocumentFragment.addEventListener

Call Signature

addEventListener(
type,
listener,
options?
): void;

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

Parameters
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | AddEventListenerOptions
Returns

void

Overrides
DocumentFragment.addEventListener

append()

append(...nodes): void;

Inserts nodes after the last child of node, while replacing strings in nodes with equivalent Text nodes.

Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.

MDN Reference

Parameters

ParameterType
...nodes(string | Node)[]

Returns

void

Inherited from

DocumentFragment.append


appendChild()

appendChild<T>(node): T;

The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node.

MDN Reference

Type Parameters

Type Parameter
T extends Node

Parameters

ParameterType
nodeT

Returns

T

Inherited from

DocumentFragment.appendChild


cloneNode()

cloneNode(subtree?): Node;

The cloneNode() method of the Node interface returns a duplicate of the node on which this method was called. Its parameter controls if the subtree contained in the node is also cloned or not.

MDN Reference

Parameters

ParameterType
subtree?boolean

Returns

Node

Inherited from

DocumentFragment.cloneNode


compareDocumentPosition()

compareDocumentPosition(other): number;

The compareDocumentPosition() method of the Node interface reports the position of its argument node relative to the node on which it is called.

MDN Reference

Parameters

ParameterType
otherNode

Returns

number

Inherited from

DocumentFragment.compareDocumentPosition


contains()

contains(other): boolean;

The contains() method of the Node interface returns a boolean value indicating whether a node is a descendant of a given node, that is the node itself, one of its direct children (childNodes), one of the children's direct children, and so on.

MDN Reference

Parameters

ParameterType
otherNode | null

Returns

boolean

Inherited from

DocumentFragment.contains


dispatchEvent()

dispatchEvent(event): boolean;

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

MDN Reference

Parameters

ParameterType
eventEvent

Returns

boolean

Inherited from

DocumentFragment.dispatchEvent


elementFromPoint()

elementFromPoint(x, y): Element | null;

Parameters

ParameterType
xnumber
ynumber

Returns

Element | null

Inherited from

DocumentOrShadowRoot.elementFromPoint


elementsFromPoint()

elementsFromPoint(x, y): Element[];

Parameters

ParameterType
xnumber
ynumber

Returns

Element[]

Inherited from

DocumentOrShadowRoot.elementsFromPoint


getAnimations()

getAnimations(): Animation[];

MDN Reference

Returns

Animation[]

Inherited from

DocumentOrShadowRoot.getAnimations


getElementById()

getElementById(elementId): HTMLElement | null;

The getElementById() method of the DocumentFragment returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

Parameters

ParameterType
elementIdstring

Returns

HTMLElement | null

Inherited from

DocumentFragment.getElementById


getHTML()

getHTML(options?): string;

The getHTML() method of the ShadowRoot interface is used to serialize a shadow root's DOM to an HTML string.

MDN Reference

Parameters

ParameterType
options?GetHTMLOptions

Returns

string


getRootNode()

getRootNode(options?): Node;

The getRootNode() method of the Node interface returns the context object's root, which optionally includes the shadow root if it is available.

MDN Reference

Parameters

ParameterType
options?GetRootNodeOptions

Returns

Node

Inherited from

DocumentFragment.getRootNode


hasChildNodes()

hasChildNodes(): boolean;

The hasChildNodes() method of the Node interface returns a boolean value indicating whether the given Node has child nodes or not.

MDN Reference

Returns

boolean

Inherited from

DocumentFragment.hasChildNodes


insertBefore()

insertBefore<T>(node, child): T;

The insertBefore() method of the Node interface inserts a node before a reference node as a child of a specified parent node.

MDN Reference

Type Parameters

Type Parameter
T extends Node

Parameters

ParameterType
nodeT
childNode | null

Returns

T

Inherited from

DocumentFragment.insertBefore


isDefaultNamespace()

isDefaultNamespace(namespace): boolean;

The isDefaultNamespace() method of the Node interface accepts a namespace URI as an argument. It returns a boolean value that is true if the namespace is the default namespace on the given node and false if not. The default namespace can be retrieved with Node.lookupNamespaceURI() by passing null as the argument.

MDN Reference

Parameters

ParameterType
namespacestring | null

Returns

boolean

Inherited from

DocumentFragment.isDefaultNamespace


isEqualNode()

isEqualNode(otherNode): boolean;

The isEqualNode() method of the Node interface tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that must match varies depending on the types of the nodes.

MDN Reference

Parameters

ParameterType
otherNodeNode | null

Returns

boolean

Inherited from

DocumentFragment.isEqualNode


isSameNode()

isSameNode(otherNode): boolean;

The isSameNode() method of the Node interface is a legacy alias the for the === strict equality operator. That is, it tests whether two nodes are the same (in other words, whether they reference the same object).

MDN Reference

Parameters

ParameterType
otherNodeNode | null

Returns

boolean

Inherited from

DocumentFragment.isSameNode


lookupNamespaceURI()

lookupNamespaceURI(prefix): string | null;

The lookupNamespaceURI() method of the Node interface takes a prefix as parameter and returns the namespace URI associated with it on the given node if found (and null if not). This method's existence allows Node objects to be passed as a namespace resolver to XPathEvaluator.createExpression() and XPathEvaluator.evaluate().

MDN Reference

Parameters

ParameterType
prefixstring | null

Returns

string | null

Inherited from

DocumentFragment.lookupNamespaceURI


lookupPrefix()

lookupPrefix(namespace): string | null;

The lookupPrefix() method of the Node interface returns a string containing the prefix for a given namespace URI, if present, and null if not. When multiple prefixes are possible, the first prefix is returned.

MDN Reference

Parameters

ParameterType
namespacestring | null

Returns

string | null

Inherited from

DocumentFragment.lookupPrefix


moveBefore()

moveBefore(node, child): void;

MDN Reference

Parameters

ParameterType
nodeNode
childNode | null

Returns

void

Inherited from

DocumentFragment.moveBefore


normalize()

normalize(): void;

The normalize() method of the Node interface puts the specified node and all of its sub-tree into a normalized form. In a normalized sub-tree, no text nodes in the sub-tree are empty and there are no adjacent text nodes.

MDN Reference

Returns

void

Inherited from

DocumentFragment.normalize


prepend()

prepend(...nodes): void;

Inserts nodes before the first child of node, while replacing strings in nodes with equivalent Text nodes.

Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.

MDN Reference

Parameters

ParameterType
...nodes(string | Node)[]

Returns

void

Inherited from

DocumentFragment.prepend


querySelector()

Call Signature

querySelector<K>(selectors): HTMLElementTagNameMap[K] | null;

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

Type Parameters
Type Parameter
K extends keyof HTMLElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

HTMLElementTagNameMap[K] | null

Inherited from

DocumentFragment.querySelector

Call Signature

querySelector<K>(selectors): SVGElementTagNameMap[K] | null;
Type Parameters
Type Parameter
K extends keyof SVGElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

SVGElementTagNameMap[K] | null

Inherited from

DocumentFragment.querySelector

Call Signature

querySelector<K>(selectors): MathMLElementTagNameMap[K] | null;
Type Parameters
Type Parameter
K extends keyof MathMLElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

MathMLElementTagNameMap[K] | null

Inherited from

DocumentFragment.querySelector

Call Signature

querySelector<K>(selectors):
| HTMLElementDeprecatedTagNameMap[K]
| null;
Type Parameters
Type Parameter
K extends keyof HTMLElementDeprecatedTagNameMap
Parameters
ParameterType
selectorsK
Returns

| HTMLElementDeprecatedTagNameMap[K] | null

Deprecated
Inherited from

DocumentFragment.querySelector

Call Signature

querySelector<E>(selectors): E | null;
Type Parameters
Type ParameterDefault type
E extends ElementElement
Parameters
ParameterType
selectorsstring
Returns

E | null

Inherited from

DocumentFragment.querySelector


querySelectorAll()

Call Signature

querySelectorAll<K>(selectors): NodeListOf<HTMLElementTagNameMap[K]>;

Returns all element descendants of node that match selectors.

MDN Reference

Type Parameters
Type Parameter
K extends keyof HTMLElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

NodeListOf<HTMLElementTagNameMap[K]>

Inherited from

DocumentFragment.querySelectorAll

Call Signature

querySelectorAll<K>(selectors): NodeListOf<SVGElementTagNameMap[K]>;
Type Parameters
Type Parameter
K extends keyof SVGElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

NodeListOf<SVGElementTagNameMap[K]>

Inherited from

DocumentFragment.querySelectorAll

Call Signature

querySelectorAll<K>(selectors): NodeListOf<MathMLElementTagNameMap[K]>;
Type Parameters
Type Parameter
K extends keyof MathMLElementTagNameMap
Parameters
ParameterType
selectorsK
Returns

NodeListOf<MathMLElementTagNameMap[K]>

Inherited from

DocumentFragment.querySelectorAll

Call Signature

querySelectorAll<K>(selectors): NodeListOf<HTMLElementDeprecatedTagNameMap[K]>;
Type Parameters
Type Parameter
K extends keyof HTMLElementDeprecatedTagNameMap
Parameters
ParameterType
selectorsK
Returns

NodeListOf<HTMLElementDeprecatedTagNameMap[K]>

Deprecated
Inherited from

DocumentFragment.querySelectorAll

Call Signature

querySelectorAll<E>(selectors): NodeListOf<E>;
Type Parameters
Type ParameterDefault type
E extends ElementElement
Parameters
ParameterType
selectorsstring
Returns

NodeListOf<E>

Inherited from

DocumentFragment.querySelectorAll


removeChild()

removeChild<T>(child): T;

The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node.

MDN Reference

Type Parameters

Type Parameter
T extends Node

Parameters

ParameterType
childT

Returns

T

Inherited from

DocumentFragment.removeChild


removeEventListener()

Call Signature

removeEventListener<K>(
type,
listener,
options?
): void;

The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

MDN Reference

Type Parameters
Type Parameter
K extends "slotchange"
Parameters
ParameterType
typeK
listener(this, ev) => any
options?boolean | EventListenerOptions
Returns

void

Overrides

DocumentFragment.removeEventListener

Call Signature

removeEventListener(
type,
listener,
options?
): void;

The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

MDN Reference

Parameters
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | EventListenerOptions
Returns

void

Overrides
DocumentFragment.removeEventListener

replaceChild()

replaceChild<T>(node, child): T;

The replaceChild() method of the Node interface replaces a child node within the given (parent) node.

MDN Reference

Type Parameters

Type Parameter
T extends Node

Parameters

ParameterType
nodeNode
childT

Returns

T

Inherited from

DocumentFragment.replaceChild


replaceChildren()

replaceChildren(...nodes): void;

Replace all children of node with nodes, while replacing strings in nodes with equivalent Text nodes.

Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.

MDN Reference

Parameters

ParameterType
...nodes(string | Node)[]

Returns

void

Inherited from

DocumentFragment.replaceChildren


setHTMLUnsafe()

setHTMLUnsafe(html): void;

The setHTMLUnsafe() method of the ShadowRoot interface can be used to parse HTML input into a DocumentFragment, optionally filtering out unwanted elements and attributes, and then use it to replace the existing tree in the Shadow DOM.

MDN Reference

Parameters

ParameterType
htmlstring

Returns

void

Properties

activeElement

readonly activeElement: Element | null;

Returns the deepest element in the document through which or to which key events are being routed. This is, roughly speaking, the focused element in the document.

For the purposes of this API, when a child browsing context is focused, its container is focused in the parent browsing context. For example, if the user moves the focus to a text control in an iframe, the iframe is the element returned by the activeElement API in the iframe's node document.

Similarly, when the focused element is in a different node tree than documentOrShadowRoot, the element returned will be the host that's located in the same node tree as documentOrShadowRoot if documentOrShadowRoot is a shadow-including inclusive ancestor of the focused element, and null if not.

MDN Reference

Inherited from

DocumentOrShadowRoot.activeElement


adoptedStyleSheets

adoptedStyleSheets: CSSStyleSheet[];

MDN Reference

Inherited from

DocumentOrShadowRoot.adoptedStyleSheets


ATTRIBUTE_NODE

readonly ATTRIBUTE_NODE: 2;

Inherited from

DocumentFragment.ATTRIBUTE_NODE


baseURI

readonly baseURI: string;

The read-only baseURI property of the Node interface returns the absolute base URL of the document containing the node.

MDN Reference

Inherited from

DocumentFragment.baseURI


CDATA_SECTION_NODE

readonly CDATA_SECTION_NODE: 4;

node is a CDATASection node.

Inherited from

DocumentFragment.CDATA_SECTION_NODE


childElementCount

readonly childElementCount: number;

MDN Reference

Inherited from

DocumentFragment.childElementCount


childNodes

readonly childNodes: NodeListOf<ChildNode>;

The read-only childNodes property of the Node interface returns a live NodeList of child nodes of the given element where the first child node is assigned index 0. Child nodes include elements, text and comments.

MDN Reference

Inherited from

DocumentFragment.childNodes


children

readonly children: HTMLCollection;

Returns the child elements.

MDN Reference

Inherited from

DocumentFragment.children


clonable

readonly clonable: boolean;

The clonable read-only property of the ShadowRoot interface returns true if the shadow root is clonable, and false otherwise.

MDN Reference


COMMENT_NODE

readonly COMMENT_NODE: 8;

node is a Comment node.

Inherited from

DocumentFragment.COMMENT_NODE


customElementRegistry

readonly customElementRegistry: CustomElementRegistry | null;

Inherited from

DocumentOrShadowRoot.customElementRegistry


delegatesFocus

readonly delegatesFocus: boolean;

The delegatesFocus read-only property of the ShadowRoot interface returns true if the shadow root delegates focus, and false otherwise.

MDN Reference


DOCUMENT_FRAGMENT_NODE

readonly DOCUMENT_FRAGMENT_NODE: 11;

node is a DocumentFragment node.

Inherited from

DocumentFragment.DOCUMENT_FRAGMENT_NODE


DOCUMENT_NODE

readonly DOCUMENT_NODE: 9;

node is a document.

Inherited from

DocumentFragment.DOCUMENT_NODE


DOCUMENT_POSITION_CONTAINED_BY

readonly DOCUMENT_POSITION_CONTAINED_BY: 16;

Set when other is a descendant of node.

Inherited from

DocumentFragment.DOCUMENT_POSITION_CONTAINED_BY


DOCUMENT_POSITION_CONTAINS

readonly DOCUMENT_POSITION_CONTAINS: 8;

Set when other is an ancestor of node.

Inherited from

DocumentFragment.DOCUMENT_POSITION_CONTAINS


DOCUMENT_POSITION_DISCONNECTED

readonly DOCUMENT_POSITION_DISCONNECTED: 1;

Set when node and other are not in the same tree.

Inherited from

DocumentFragment.DOCUMENT_POSITION_DISCONNECTED


DOCUMENT_POSITION_FOLLOWING

readonly DOCUMENT_POSITION_FOLLOWING: 4;

Set when other is following node.

Inherited from

DocumentFragment.DOCUMENT_POSITION_FOLLOWING


DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC

readonly DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32;

Inherited from

DocumentFragment.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC


DOCUMENT_POSITION_PRECEDING

readonly DOCUMENT_POSITION_PRECEDING: 2;

Set when other is preceding node.

Inherited from

DocumentFragment.DOCUMENT_POSITION_PRECEDING


DOCUMENT_TYPE_NODE

readonly DOCUMENT_TYPE_NODE: 10;

node is a doctype.

Inherited from

DocumentFragment.DOCUMENT_TYPE_NODE


ELEMENT_NODE

readonly ELEMENT_NODE: 1;

node is an element.

Inherited from

DocumentFragment.ELEMENT_NODE


ENTITY_NODE

readonly ENTITY_NODE: 6;

Inherited from

DocumentFragment.ENTITY_NODE


ENTITY_REFERENCE_NODE

readonly ENTITY_REFERENCE_NODE: 5;

Inherited from

DocumentFragment.ENTITY_REFERENCE_NODE


firstChild

readonly firstChild: ChildNode | null;

The read-only firstChild property of the Node interface returns the node's first child in the tree, or null if the node has no children.

MDN Reference

Inherited from

DocumentFragment.firstChild


firstElementChild

readonly firstElementChild: Element | null;

Returns the first child that is an element, and null otherwise.

MDN Reference

Inherited from

DocumentFragment.firstElementChild


fullscreenElement

readonly fullscreenElement: Element | null;

Returns document's fullscreen element.

MDN Reference

Inherited from

DocumentOrShadowRoot.fullscreenElement


host

readonly host: Element;

The host read-only property of the ShadowRoot returns a reference to the DOM element the ShadowRoot is attached to.

MDN Reference


innerHTML

innerHTML: string;

The innerHTML property of the ShadowRoot interface gets or sets the HTML markup to the DOM tree inside the ShadowRoot.

MDN Reference


isConnected

readonly isConnected: boolean;

The read-only isConnected property of the Node interface returns a boolean indicating whether the node is connected (directly or indirectly) to a Document object.

MDN Reference

Inherited from

DocumentFragment.isConnected


lastChild

readonly lastChild: ChildNode | null;

The read-only lastChild property of the Node interface returns the last child of the node, or null if there are no child nodes.

MDN Reference

Inherited from

DocumentFragment.lastChild


lastElementChild

readonly lastElementChild: Element | null;

Returns the last child that is an element, and null otherwise.

MDN Reference

Inherited from

DocumentFragment.lastElementChild


mode

readonly mode: ShadowRootMode;

The mode read-only property of the ShadowRoot specifies its mode — either open or closed. This defines whether or not the shadow root's internal features are accessible from JavaScript.

MDN Reference


nextSibling

readonly nextSibling: ChildNode | null;

The read-only nextSibling property of the Node interface returns the node immediately following the specified one in their parent's childNodes, or returns null if the specified node is the last child in the parent element.

MDN Reference

Inherited from

DocumentFragment.nextSibling


nodeName

readonly nodeName: string;

The read-only nodeName property of Node returns the name of the current node as a string.

MDN Reference

Inherited from

DocumentFragment.nodeName


nodeType

readonly nodeType: number;

The read-only nodeType property of a Node interface is an integer that identifies what the node is. It distinguishes different kinds of nodes from each other, such as elements, text, and comments.

MDN Reference

Inherited from

DocumentFragment.nodeType


nodeValue

nodeValue: string | null;

The nodeValue property of the Node interface returns or sets the value of the current node.

MDN Reference

Inherited from

DocumentFragment.nodeValue


NOTATION_NODE

readonly NOTATION_NODE: 12;

Inherited from

DocumentFragment.NOTATION_NODE


onslotchange

onslotchange: ((this, ev) => any) | null;

ownerDocument

readonly ownerDocument: Document;

The read-only ownerDocument property of the Node interface returns the top-level document object of the node.

MDN Reference

Inherited from

DocumentFragment.ownerDocument


parentElement

readonly parentElement: HTMLElement | null;

The read-only parentElement property of Node interface returns the DOM node's parent Element, or null if the node either has no parent, or its parent isn't a DOM Element. Node.parentNode on the other hand returns any kind of parent, regardless of its type.

MDN Reference

Inherited from

DocumentFragment.parentElement


parentNode

readonly parentNode: ParentNode | null;

The read-only parentNode property of the Node interface returns the parent of the specified node in the DOM tree.

MDN Reference

Inherited from

DocumentFragment.parentNode


pictureInPictureElement

readonly pictureInPictureElement: Element | null;

MDN Reference

Inherited from

DocumentOrShadowRoot.pictureInPictureElement


pointerLockElement

readonly pointerLockElement: Element | null;

MDN Reference

Inherited from

DocumentOrShadowRoot.pointerLockElement


previousSibling

readonly previousSibling: ChildNode | null;

The read-only previousSibling property of the Node interface returns the node immediately preceding the specified one in its parent's childNodes list, or null if the specified node is the first in that list.

MDN Reference

Inherited from

DocumentFragment.previousSibling


PROCESSING_INSTRUCTION_NODE

readonly PROCESSING_INSTRUCTION_NODE: 7;

node is a ProcessingInstruction node.

Inherited from

DocumentFragment.PROCESSING_INSTRUCTION_NODE


serializable

readonly serializable: boolean;

The serializable read-only property of the ShadowRoot interface returns true if the shadow root is serializable.

MDN Reference


slotAssignment

readonly slotAssignment: SlotAssignmentMode;

The read-only slotAssignment property of the ShadowRoot interface returns the slot assignment mode for the shadow DOM tree. Nodes are either automatically assigned (named) or manually assigned (manual). The value of this property defined using the slotAssignment option when calling Element.attachShadow().

MDN Reference


styleSheets

readonly styleSheets: StyleSheetList;

MDN Reference

Inherited from

DocumentOrShadowRoot.styleSheets


TEXT_NODE

readonly TEXT_NODE: 3;

node is a Text node.

Inherited from

DocumentFragment.TEXT_NODE