HTML DOM Body object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Body object is associated with the HTML <body> element. The attributes and their values set inside the body tag stay throughout the HTML document unless they are overridden by any of its child node.The body object can be used to access and manipulate these properties and their values.

Properties

Following are the properties for HTML DOM Body object −

Note − The properties below are not supported in HTML5 .Use CSS instead −

PropertyDesciption
aLinkTo set or return the active link color in a document.
backgroundTo set or return the background image for the document
bgColorTo set or return the background color of the document.
linkTo set or return the unvisited links color in the document.
textTo set or returns the text color of the document
vLinkTo set or return the visited links color in the document.

Syntax

Following is the syntax for −

Creating the body object −

var x = document.createElement("BODY");

Example

Let us see an example for HTML DOM body object −

<!DOCTYPE html>
<html>
<head>
<title>aside tag</title>
<h2>HTML DOM BODY OBJECT</h2>
<button onclick="createBody()">CREATE</button>
<script>
   function createBody() {
      var x = document.createElement("BODY");
      x.setAttribute("id", "myBody");
      document.body.appendChild(x);
      var Para = document.createElement("P");
      var text = document.createTextNode("A sample paragraph
      created as child of body");
      Para.appendChild(text);
      document.getElementById("myBody").appendChild(Para);
   }
</script>
</head>
</html>

Output

This will produce the following output −

On clicking create −

In the above example −

We have created a button CREATE to call function createBody() −

<button onclick="createBody()">CREATE</button>

The createBody() method creates the element <body> and assigns it to the variable x. Using the setAttribute() method we set the id of the body to “myBody” and append the body element using appendChild() method as the child of the document. Another element <p> (paragraph) is then created with textNode “A sample paragraph created as child of body”. That text node is appended as child of the paragraph. Finally using the id of the <body> element the paragraph is appended to the body.

function createBody() {
   var x = document.createElement("BODY");
   x.setAttribute("id", "myBody");
   document.body.appendChild(x);
   var Para = document.createElement("P");
   var text = document.createTextNode("A sample paragraph created as child of body");
   Para.appendChild(text);
   document.getElementById("myBody").appendChild(Para);
}
raja
Published on 06-Aug-2019 13:58:25
Advertisements