HTML DOM Body property

HTMLWeb DevelopmentFront End Technology

The HTML DOM body property associated with HTML <body> element is used to set or return property values of the <body> element. It returns the <body> element. It can be used to change content inside the <body> element. This property can overwrite child elements content present inside the <body> element.


Following is the syntax for −

Setting the body property −

document.body = New_Content

Here, New_Content is the new content for the element.


Let us see an example for the HTML DOM Body property −

Live Demo

<!DOCTYPE html>
<h2>Sample HEADING</h2>
<p>Click the below button to overwrite child content</p>
<button onclick="OverWrite()">Overwrite Content</button>
<p>A sample paragraph</p>
   function OverWrite() {
      document.body.innerHTML = "All the content inside the body tag is overwritten";


This will produce the following output −

On clicking “Overwrite Content” −

In the above example −

We have various elements inside the <body> element like a <h2> element, two <p> elements and a button. The “Overwrite Content” button executes the OverWrite() function.

<button>Overwrite Content</button>

The OverWrite() function gets the innerHtml of the document body and changes the text to “All the content inside the body tag is overwritten” . This overwrites all the content inside body basically removing all the elements and letting only the text display.

function OverWrite() {
   document.body.innerHTML = "All the content inside the body tag is overwritten";
Updated on 20-Feb-2021 06:15:52