The Wayback Machine - https://web.archive.org/web/20250406084455/https://www.geeksforgeeks.org/html-dom-documentelement-property/
Open In App

HTML DOM documentElement Property

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
News Follow

The DOM documentElement property is used to return the documentElement as an Element Object. It is a read-only property. It returns a <HTML> Element. 

Syntax:

document.documentElement 

Return Value: It returns the documentElement of the document or an element object. 

Example: Below program illustrates the documentElement property in HTML: 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        DOM documentElement Property
    </title>

    <script>
        function Geeks() {
            let x = document.documentElement.nodeName;
            document.getElementById("sudo").innerHTML = x;
        }
    </script>
</head>

<body>
    <center>
        <h1>GeeksforGeeks</h1>
        <h2>DOM documentElement Property</h2>
        <button onclick="Geeks()">
            Submit
        </button>
        <p id="sudo"></p>
    </center>
</body>

</html>

Output: 

Supported Browsers: The browser supported by DOM documentElement property are listed below:

HTML DOM documentElement Property – FAQs

Can documentElement be null?

documentElement will be null if the document is not fully loaded or if it does not have a root element, which is rare in typical HTML documents.

How to change the CSS style of the documentElement?

You can change the CSS style by accessing the style property, e.g., document.documentElement.style.backgroundColor = ‘blue’;.

What are common uses of documentElement?

Common uses include changing global styles, scrolling the entire page, and accessing document dimensions like document.documentElement.clientWidth.

How to get the scroll position using documentElement?

You can get the scroll position with document.documentElement.scrollTop for vertical scroll and document.documentElement.scrollLeft for horizontal scroll.

How does documentElement relate to other DOM properties?

documentElement is the root of the document, so it’s the parent of both document.head and document.body. It provides a way to access the entire document structure.

What are the cross-browser considerations for documentElement?

Generally, documentElement works consistently across modern browsers, but older versions of Internet Explorer (before IE9) may require different handling for scroll properties.


Next Article

Similar Reads

three90RightbarBannerImg