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: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)
<!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>
<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
Post a Comment