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 access by using document.body object.
Object Properties:

  • Alink: It is used to sets or returns the color of the active link in a Document.
  • background: It is used to sets or returns the background image for a document.
  • bgColor: It is used to sets or returns the backgroundColor for the document.
  • link: It is used to sets or returns the color of he unvisited link in the document.
  • text: It is used to sets or returns the color of a text in a document.
  • vLink: It is used to sets or returns the color of the visited link in a document.

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:
Before Clicking On Button:

After Clicking on Button:

Example-2: Body Object can be created by using the document.createElement Method.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>aside tag</title>
    <style>
        h2 {
            font-size: 40px;
        }
          
        .gfg {
            font-size: 30px;
            text-align: center;
        }
          
        h1 {
            text-align: center;
        }
          
        p {
            font-size: 20px;
            margin: 20px 0;
            text-align: center;
        }
    </style>
    <center>
        <H2>DOM Body Object</H2>
        <button onclick="myGeeks()">Submit</button>
        <script>
            function myGeeks() {
                var w = document.createElement("BODY");
                w.setAttribute("id", "GFG");
                document.body.appendChild(w);
  
                var heading = document.createElement("H1");
                var text1 = document.createTextNode("GeeksForGeeks");
                heading.appendChild(text1);
                document.getElementById("GFG").appendChild(heading);
  
                var para = document.createElement("P");
                var text2 = 
                document.createTextNode("A Computer Science Portal"
                +" for Geeks.");
                para.appendChild(text2);
                document.getElementById("GFG").appendChild(para);
            }
        </script>
  
</head>
  
</html>                   

chevron_right


Before Clicking On Button:

After Clicking on Button:

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

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.