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
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
- 1 That makes no sense: jsfiddle/9FzAM – Ian Commented Jul 15, 2014 at 18:17
- 1 @Ian your example is the regular DOM; this question is about shadow-DOM (enable in chrome devtools.) – Evan Davis Commented Jul 15, 2014 at 18:22
-
@Mathletics A ha! I seriously had no idea what they meant by "shadow dom" (the
#
made me think "id"), and somehow missed the "shadow-dom" and "polymer" tags. Thanks for pointing that out – Ian Commented Jul 15, 2014 at 18:23 -
3
shadowRoot.host
would give you the<div>
. Likewise,div.shadowRoot
would get you the div's shadow root. – ebidel Commented Jul 15, 2014 at 18:25 -
1
@ebidel Good call. You can see
host
andparentNode
properties of the shadow root withconsole.dir(child.parentNode);
– Ian Commented Jul 15, 2014 at 18:27
2 Answers
Reset to default 6You can use shadowRoot.host
to give you the <div>
. Likewise, div.shadowRoot
would get you the div's shadow root.
First find shadowRoot host from the element el.getRootNode().host
and then use closest()
el.getRootNode().host.closest('div.my_div')