Nearby lessons

12 of 14

JavaScript HTML DOM Elements (Nodes)

📌 What are DOM Nodes?

JavaScript allows you to dynamically create, remove, or replace HTML elements (nodes) in the DOM. These operations are useful when building interactive applications that update the page without reloading it.

🆕 Creating New HTML Elements (Nodes)

You can create a new element using document.createElement() and then append it using appendChild().

Example02
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

📌 Creating new HTML Elements - insertBefore()

Use insertBefore() to insert a new node before an existing child node.

parent.insertBefore(newNode, existingNode)

Example03
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

❌ Removing Existing HTML Elements

You can remove an element using element.remove().

Example04
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

👶 Removing a Child Node

To remove a specific child node, use parent.removeChild(child).

Example05
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔁 Replacing HTML Elements

You can replace an existing child node with another using parent.replaceChild(newNode, oldNode).

Example06
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3