user578895
user578895

Reputation:

How do I traverse up out of shadow dom?

I have a structure:

<div>
    #shadow-root
        <span>

How do I reference the div from the span?

span.parentNode is the #shadow-root, and .parentNode from that is null

Upvotes: 5

Views: 2640

Answers (2)

GorvGoyl
GorvGoyl

Reputation: 49150

First find shadowRoot host from the element el.getRootNode().host and then use closest()

el.getRootNode().host.closest('div.my_div')

Upvotes: 0

ebidel
ebidel

Reputation: 24109

You can use shadowRoot.host to give you the <div>. Likewise, div.shadowRoot would get you the div's shadow root.

Upvotes: 6

Related Questions