Element appendChild() Method - Create a <p> element and append it to a <div> element: - Javascript DOM

Javascript examples for DOM:Element appendChild

Description

Element appendChild() Method - Create a <p> element and append it to a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//  w  ww .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");
    var t = document.createTextNode("This is a paragraph.");
    para.appendChild(t);
    document.getElementById("myDIV").appendChild(para);
}
</script>

</body>
</html>

Related Tutorials