JavaScript DOM Nodes

Back to JS page

Table of Contents

Creating New HTML Elements

To create new HTML elements, use document.createElement() and appendChild():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript DOM Nodes</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Creating New HTML Elements</h4>

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

<button onclick="createElement()">Create New Paragraph</button>

<script>
function createElement() {
  const para = document.createElement("p");
  const node = document.createTextNode("This is new.");
  para.appendChild(node);
  document.getElementById("div1").appendChild(para);
}
</script>

</body>
</html>

Example 1

Result View Example

insertBefore()

You can insert a new element before an existing element using insertBefore():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript DOM Nodes</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>insertBefore()</h4>

<div id="div2">
  <p id="p3">First paragraph</p>
  <p id="p4">Second paragraph</p>
</div>

<button onclick="insertElement()">Insert Before First</button>

<script>
function insertElement() {
  const para = document.createElement("p");
  const node = document.createTextNode("Inserted paragraph.");
  para.appendChild(node);

  const parent = document.getElementById("div2");
  const child = document.getElementById("p3");
  parent.insertBefore(para, child);
}
</script>

</body>
</html>

Example 2

Result View Example

Removing Existing HTML Elements

Use remove() to delete an HTML element:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript DOM Nodes</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Removing Existing HTML Elements</h4>

<div id="div3">
  <p id="p5">Paragraph 1</p>
  <p id="p6">Paragraph 2 - will be removed</p>
  <p id="p7">Paragraph 3</p>
</div>

<button onclick="removeElement()">Remove Paragraph 2</button>

<script>
function removeElement() {
  const element = document.getElementById("p6");
  element.remove();
}
</script>

</body>
</html>

Example 3

Result View Example

Removing a Child Node

For older browsers that do not support remove(), you can use removeChild():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript DOM Nodes</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Removing a Child Node</h4>

<div id="div4">
  <p id="p8">Item A</p>
  <p id="p9">Item B - will be removed via parent</p>
  <p id="p10">Item C</p>
</div>

<button onclick="removeChildElement()">Remove Item B</button>

<script>
function removeChildElement() {
  const parent = document.getElementById("div4");
  const child = document.getElementById("p9");
  parent.removeChild(child);
}
</script>

</body>
</html>

Example 4

Result View Example

Replacing HTML Elements

Use replaceChild() to replace an element with another:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript DOM Nodes</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Replacing HTML Elements</h4>

<div id="div5">
  <p id="p11">Original paragraph</p>
</div>

<button onclick="replaceElement()">Replace Paragraph</button>

<script>
function replaceElement() {
  const para = document.createElement("p");
  const node = document.createTextNode("Replaced paragraph!");
  para.appendChild(node);

  const parent = document.getElementById("div5");
  const child = document.getElementById("p11");
  parent.replaceChild(para, child);
}
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference