Web APIs

Element.prototype.attachShadow()

Try the free JavaScript compiler

Attaches a shadow root to an eligible host; returns the root.

Formal Syntax & Structure

javascript syntaxjavascript
host.attachShadow({mode: "open" | "closed"})

Parameters & Attributes

NameTypeDescription
optionsShadowRootInitRequired mode and optional shadow configuration.

Return Value

ShadowRoot

Attaches a shadow root to an eligible host; returns the root.

Working Example

Example Snippetjavascript
(async () => {const host=document.createElement("div");document.body.append(host);const root=host.attachShadow({mode:"open"});const span=document.createElement("span");span.id="lesson-inner";span.textContent="ready";root.append(span);console.log(document.querySelector("#lesson-inner")===null,root.querySelector("#lesson-inner").textContent);})()
JavaScript Reference