Skip to main content

HTML DOM createElement() Method in JAVA SCRIPT

Definition and Usage..
The createElement() method creates an Element Node with the specified name.
Tip: Use the createTextNode() method to create a text node.
Tip: After the element is created, use the element.appendChild() or element.insertBefore() method to insert it to the document.
Syntax: document.createElement(nodename)
CODING:
<!DOCTYPE html>
<html>
<body>
<div id="myDIV">
A DIV element
</div>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
    var para = document.createElement("P");
    para.innerHTML="This is a paragraph.";
    //var t = document.createTextNode("This is a paragraph.");
    //para.appendChild(t);
    document.getElementById("myDIV").appendChild(para);
}
</script>
</body>
</html>

Comments