Javascript DOM HTML Document body Property append to body element

Introduction

Create a <p> element with some text and append it to the document's body:

Click the button to create a P element with some text, and append it to the document's body.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*ww  w .  jav a  2 s.c  om*/
  var x = document.createElement("P");
  var t = document.createTextNode("This is a paragraph.");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related