Document body Property - Javascript DOM

Javascript examples for DOM:Document body

Description

The body property sets or gets the document's body.

It returns the <body> element of the current document.

When setting a new value, this property overwrites all child elements inside the existing <body> element.

document.body returns the <body> element, while the document.documentElement returns the <html> element.

Set the body property with the following Values

Value Description
newContent Sets the new content for <body>

Return Value

A reference to the Body Object, which represents a <body> element

The following code shows how to change the background color of the current document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from w  w  w .  j a  va  2 s .  c  o m*/
    document.body.style.backgroundColor = "yellow";
}
</script>

</body>
</html>

Related Tutorials