HTML DOM body property
Example
Modify the background color of the current document:
document
.
body
.
style
.
backgroundColor
=
"
yellow
"
;
Try it »
Definition and usage
The body property is used to set or return the body of the document.
If returning, this property returns the current document's .
If setting, this property overwrites all child elements within the element and replaces them with new content.
Tip: The difference between document.documentElement Unlike the property, the document.body property returns the element, while the document.documentElement property returns the element.
Browser support
| property | |||||
|---|---|---|---|---|---|
| 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 of the element |
Technical Details
| DOM version: | Core Level 1 Document Object |
|---|---|
| Return Value: | A reference to the Body object, representing the element |
More examples
Example
Get the HTML content of the current document:
var
x
=
document
.
body
.
innerHTML
;
Try it »
Example
Modify the HTML content of the current document:
document
.
body
.
innerHTML
=
"
New content...
"
;
Try it »
Example
creation
The element and add it to the document:
var
x
=
document
.
createElement
(
"
P
"
)
;
//
creation
Element
var
t
=
document
.
createTextNode
(
"
This is the newly added paragraph.
"
)
;
//
Create a text node
x
.
appendChild
(
t
)
;
//
Text added to
element contains a
document
.
body
.
appendChild
(
x
)
;
//
data to the child component via
Added to the element
Try it »
Related pages
HTML Reference Manual: HTML Tag
JavaScript Reference Manual: HTML DOM Body object
Document Object