Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get html elements inside shadow root using javascript

I have example HTML code:

<section class="search-module">
    #shadow-root (open)
        <div class="app">
            <div class="title">Product Title</div>
        </div>
</section>

And with this code I can access to shadow root element parent container:

var searchModule = document.getElementsByClassName("search-module").item(0);

But can't get elements inside shadow root container using this command:

searchModule.getElementsByClassName("title") // undefined
like image 644
Andreas Hunter Avatar asked Aug 25 '26 22:08

Andreas Hunter


1 Answers

You have to navigate to shadow-root first then you can get it:

const searchModule = document.querySelector('.search-module');
const searchModuleRoot = searchModule && searchModule.shadowRoot;

const title = searchModuleRoot.querySelector('.title');
like image 60
Deepak Dixit Avatar answered Aug 28 '26 12:08

Deepak Dixit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!