1let myElm = document.createElement("p"); // Create a new element
2
3myElm.innerText = 'test'; // Change the text of the element
4myElm.style.color = 'red'; // Change the text color of the element
5
6document.body.appendChild(myElm); // Add the object to the DOM
1 //adding 'p' tag to body
2
3 var tag = document.createElement("p"); // <p></p>
4 var text = document.createTextNode("TEST TEXT");
5 tag.appendChild(text); // <p>TEST TEXT</p>
6 var element = document.getElementsByTagName("body")[0];
7 element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>
1<html>
2<head>
3<title>t1</title>
4<script type="text/javascript">
5 function addNode()
6 {var newP = document.createElement("p");
7 var textNode = document.createTextNode(" This is a new text node");
8 newP.appendChild(textNode);
9 document.getElementById("firstP").appendChild(newP); }
10</script> </head>
11<body> <p id="firstP">firstP<p> </body>
12</html>
13
1
2 var para = document.createElement("P"); // Create a <p> element
3
4 para.innerText = "This is a paragraph"; //
5 Insert text
6document.body.appendChild(para); // Append <p> to <body>