Javascript DOM HTML Node appendChild() Method append <p> element to <div> element

Introduction

Create a <p> element and append it to a <div> element:

Click the button to create a P element with some text, and append it to DIV.</p>

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {// ww  w. j a v  a2  s  .c  om
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>
</head>
<body>

<div id="myDIV">
A DIV element
</div>

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

<script>
function myFunction() {
  var para = document.createElement("P");
  para.innerHTML = "This is a paragraph.";
  document.getElementById("myDIV").appendChild(para);
}
</script>

</body>
</html>



PreviousNext

Related