JavaScript node.getRootNode() Method

You are Here:

JavaScript node.getRootNode() Method

The node.getRootNode() method returns the context object's root, which optionally includes the shadow root if it is available.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <script> var x = document.getElementById("point"); console.log(x.getRootNode()); </script> </body> </html>

Syntax

node.getRootNode(options)

Parameter Values

ValueTypeExplanation
options
{composed:bool}
Optional A Boolean that indicates whether the shadow root should be returned.
If true, the shadow root should be returned.
If false, the shadow root should not be returned. Default value.

Return Values

ValueExplanation
ObjectReturns the context object's root.

node.getRootNode() with Options

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point">Main Element</p> <script> var x = document.getElementById("point"); var node = document.createElement("h2"); var shadowRoot = node.attachShadow({mode: "open"}); shadowRoot.innerHTML = '<div id="myShadowChild">content</div>'; var shadowChild = shadowRoot.getElementById("myShadowChild"); console.log(x.getRootNode()); console.log(shadowChild.getRootNode({composed: false})); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author