-2

What is DOMElement.shadowRoot? What does it do? Why does it return null? What can it be used for?

document.body.shadowRoot

I tried searching but I can't understand what it does.

1 Answers1

0

It's a container for the "internal DOM" (i.e. the shadow DOM) of native elements such as <input> or <button> (or custom elements made with the web components APIs).

When you inspect <input> you only see the <input> itself:

enter image description here

But you can turn on a flag in the Chrome Dev Tools to enable the visibility of the shadow DOM:

enter image description here

You can now see how the <input> is "implemented" by the browser. For example the placeholder is contained within a <div> element:

enter image description here

customcommander
  • 17,580
  • 5
  • 58
  • 84