How to work with document.documentElement in JavaScript? Last Updated : 18 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, document.documentElement is a fundamental property that refers to the root element of an HTML document. This property provides a way to access and manipulate the entire HTML document structure. In this tutorial, we will focus on how to work with document.documentElement in JavaScript. PreviewOutputApproachAt first create a basic html structure with some css style as your needs.In the javascript part use document.documentElement to access the root element of the HTML document.Then get a reference of the button using document.getElementById() or any other method.Add an event listener to the button to handle the click event.Define a function to handle changing the background color of the root element when the button is clicked.You can define a function to handle clicks attached to the root element. This function can then check if the root element itself was clicked and display an alert if necessary.Example: To demonstrate clicking the button handling the click event attached to it using JavaScript. HTML <!DOCTYPE html> <html> <head> <title>Document Root Element Example</title> <style> .main-container{ display: flex; justify-content: center; align-items: center; flex-direction: column; margin-top: 10rem; } #changeColorButton{ padding: 10px 20px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <div class="main-container"> <h1>Welcome to Document Root Element Example</h1> <p>Click the button to change the background color of the HTML root element.</p> <button id="changeColorButton">Change Background Color</button> </div> <script> // Accessing the root element let rootElement = document.documentElement; let changeColorButton = document .getElementById("changeColorButton"); function changeBackgroundColor() { rootElement.style.backgroundColor = "lightblue"; } changeColorButton.addEventListener("click", changeBackgroundColor); // Function to handle root element click function handleRootElementClick() { alert("Root element clicked!"); } // Adding event listener to the root element rootElement.addEventListener("click", handleRootElementClick); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to work with document.documentElement in JavaScript? skaftafh Follow Improve Article Tags : JavaScript Web Technologies javaScript Similar Reads How to work with document.embeds in JavaScript ? The document. embeds is a property in JavaScript that provides access to all embedded elements such as <embed> and <object> tags within the current document. These embedded elements are typically used to display external content, such as media files (e.g., videos, audio) or interactive c 3 min read How to select DOM Elements in JavaScript ? Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo 3 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read How getElementByID works in JavaScript ? The document method getElementById() returns an element object representing the element whose id property matches with the given value. This method is used to manipulate an element on our document & is widely used in web designing to change the value of any particular element or get a particular 2 min read How to Add JavaScript in HTML Document? To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev 3 min read Like