Javascript DOM HTML Document createElement() Method append 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.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/* w  ww.jav  a 2 s .com*/
  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