Answers for "how to create a node in javascript"

0

create node in javascript

<div id="div1">
  <p id="p1">This is a paragraph.</p>
  <p id="p2">This is another paragraph.</p>
</div>

<script>
  // Create <p> element
  var para = document.createElement("p");
  // Create a text node
  var node = document.createTextNode("This is new.");
  // Append text node to <p> element
  para.appendChild(node);

  // Append <p> to div element
  var element = document.getElementById("div1");
  element.appendChild(para);
</script>
Posted by: Guest on April-07-2021
1

document create element

document.body.onload = addElement;

function addElement () { 
  // create a new div element 
  var newDiv = document.createElement("div"); 
  // and give it some content 
  var newContent = document.createTextNode("Hi there and greetings!"); 
  // add the text node to the newly created div
  newDiv.appendChild(newContent);  

  // add the newly created element and its content into the DOM 
  var currentDiv = document.getElementById("div1"); 
  document.body.insertBefore(newDiv, currentDiv); 
}
Posted by: Guest on April-28-2020

Code answers related to "how to create a node in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language