JavaScript document.body Property
You are Here:
JavaScript document.body Property
The document.body
property gets or sets the document's body.
Set a CSS Property
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>I'm red</p>
<script>
document.body.style.color = "red";
</script>
</body>
</html>
Syntax
document.body
Return Value
Value | Explanation |
---|---|
Object | Returns the document's body. |
Set a HTML Element
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
document.body.innerHTML = "<h2>I'm Heading</h2>";
</script>
</body>
</html>
Set a Text Element
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
document.body.innerText = "<h2>I'm Text</h2>";
</script>
</body>
</html>
Append a HTML Element
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var node = document.createElement("h2");
var textnode = document.createTextNode("I'm Heading");
node.appendChild(textnode);
document.body.appendChild(node);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.