Easy Tutorial
❮ Jsref Todatestring Prop Textarea Readonly ❯

HTML DOM body Property

Document Object

Example

Change the background color of the current document:

document.body.style.backgroundColor = "yellow";

Definition and Usage

The body property is used to set or return the document body.

If returning, this property returns the <body> element of the current document.

If setting, this property overrides all child elements within the <body> element and replaces them with new content.

Note: Unlike the document.documentElement property, document.body returns the <body> element, while document.documentElement returns the <html> element.


Browser Support

Property Chrome Edge Firefox Safari Opera
body Yes Yes Yes Yes Yes

Syntax

Return the body property:

document.body

Set the body property:

document.body = newContent

Property

Value Description
newContent Specifies the new content for the <body>

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: A reference to the Body object, representing the <body> element
--- ---

More Examples

Example

Get the HTML content of the current document:

var x = document.body.innerHTML;

Example

Modify the HTML content of the current document:

document.body.innerHTML = "New content...";

Example

Create a <p> element and append it to the document:

var x = document.createElement("P");                        // Create a <p> element
var t = document.createTextNode("This is a new paragraph."); // Create a text node
x.appendChild(t);                                           // Append the text to the <p> element
document.body.appendChild(x);                               // Append the <p> to the <body>

Related Pages

HTML Reference: HTML <body> Tag

JavaScript Reference: HTML DOM Body Object

Document Object

❮ Jsref Todatestring Prop Textarea Readonly ❯