HTML DOM documentElement Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM documentElement property is used for returning the document element. The return type is of type Element Object. The document element is the root element of the document which in case of an HTML document will be the <HTML> element. It is a read-only property.


Following is the syntax for documentElement property −



Let us look at an example of the documentElement property −

<!DOCTYPE html>
<h1>documentElement property example<h1>
<p>Get the document element name by clicking the below button</p>
<button onclick="getDocument()">GET NAME</button>
<p id="Sample"></p>
   function getDocument() {
      var dName = document.documentElement.nodeName;
      document.getElementById("Sample").innerHTML = "The document element for this    document is: "+dName;


This will produce the following output −

On clicking the GET NAME button −

In the above example −

We have created a button GET NAME that will execute the getDocument() function on being clicked by the user.

<button onclick="getDocument()">GET NAME</button>

The getDocument() function gets the documents’ documentElement nodeName property and assigns it to the variable dName. The variable dName is then displayed in the paragraph that has id “Sample” associated with it and used the innerHTML property to display the intended text −

function getDocument() {
   var dName = document.documentElement.nodeName;
   document.getElementById("Sample").innerHTML = "The document element for this document is: "+dName;
Published on 08-Aug-2019 14:10:24