Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select element inside open Shadow DOM from Document?

Say I have a DOM that looks like this in my Document:

<body>
  <div id="outer">
    <custom-web-component>
      #shadow-root (open)
        <div id="inner">Select Me</div>
    </custom-web-component>
  </div>
</body>

Is it possible to select the inner div inside the shadow root using a single querySelector argument on document? If so, how is it constructed?

For example, something like document.querySelector('custom-web-component > #inner')

like image 803
writofmandamus Avatar asked Mar 06 '26 20:03

writofmandamus


2 Answers

You can do it like this:

document.querySelector("custom-web-component").shadowRoot.querySelector("#inner")
like image 63
Ruben Avatar answered Mar 09 '26 10:03

Ruben


It cannot be done with a single selector, that capability has been dropped from the web: https://developer.chrome.com/blog/remove-shadow-piercing/

However you can do it by calling querySelector recursively. This can be heavy so use the simplest version possible for your case.

If you know the exact root already

document.querySelector(".your-root-selector")
.shadowRoot.querySelector(".your-shadowed-selector")

If you have many roots and many shadowed elements

This will loop through every element on the page, so it's quite heavy. Use judiciously:

const elements = []
for (const {shadowRoot} of document.querySelectorAll("*")) {
    if (shadowRoot) {
        elements.push(...shadowRoot.querySelectorAll(".your-shadowed-selector"));
    }
}

If you have nested roots

This runs recursively, use rarely:

const elements = []
function findElements(root) {
    for (const {shadowRoot} of root.querySelectorAll("*")) {
        if (shadowRoot) {
            // Look for elements in the current root
            elements.push(...shadowRoot.querySelectorAll(".your-shadowed-selector"));
            // Look for more roots in the current root
            findElements(shadowRoot);
        }
    }
}
findElements(document);
like image 41
fregante Avatar answered Mar 09 '26 10:03

fregante