Javascript DOM HTML Div append <p> element to <div> element

Description

Javascript DOM HTML Div append <p> element to <div> element

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//  w  w  w  . j  a  va2 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