Open In App

HTML DOM Body Object

The DOM Body Object is used to represent the HTML <Body> element. The Body element is accessed by getElementByTagName(). It can also be accessed by using a document.body object. 

Object Properties: 



Example 1: In this example, we will use DOM body Object.




<!DOCTYPE html>
<html>
   
<head>
    <title>HTML DOM Body Object</title>
    <!-- style on the body tag -->
    <style>
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM body Object</h2>
    <button onclick="myGeeks()">Submit</button>
    <p id="sudo"></p>
   
    <script>
        function myGeeks() {
            let w = document.getElementsByTagName("BODY")[0];
            w.style.backgroundColor = "dodgerblue";
        }
    </script>
</body>
   
</html>

Output: 



 

Example 2: A body Object can be created by using the document.createElement Method. 




<!DOCTYPE html>
<html>
   
<head>
    <title>HTML DOM Body Object</title>
    <!-- style on the body tag -->
    <style>
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM body Object</h2>
    <button onclick="myGeeks()">Submit</button>
    <p id="sudo"></p>
   
    <script>
        function myGeeks() {
            let w = document.getElementsByTagName("BODY")[0];
            w.style.backgroundColor = "dodgerblue";
        }
    </script>
</body>
   
</html>

Output:

 

Supported Browsers: The browser supported by DOM Body Object are listed below: 


Article Tags :