Dynamic web applications create, insert, replace, and remove DOM nodes programmatically. JavaScript provides factory methods on document and insertion/deletion methods on DOM elements.
flowchart TD
Creation["document.createElement('div')"] --> Config["Configure attributes, text, and styles"]
Config --> Insertion{"Insertion Target Method?"}
Insertion --> Append["parent.appendChild(node)"]
Insertion --> Prepend["parent.prepend(node)"]
Insertion --> InsertBefore["parent.insertBefore(newNode, refNode)"]
Insertion --> ModernInsert["el.insertAdjacentElement('beforeend', node)"]
Insertion --> Deletion["node.remove() or parent.removeChild(node)"]
| Method | Signature | Action |
|---|---|---|
createElement() |
document.createElement("div") |
Creates an unattached element node. |
appendChild() |
parent.appendChild(child) |
Appends child node to end of parent. |
prepend() |
parent.prepend(child) |
Inserts child node at start of parent. |
insertBefore() |
parent.insertBefore(new, ref) |
Inserts new node immediately before ref node. |
remove() |
node.remove() |
Removes node directly from DOM tree. |
replaceChild() |
parent.replaceChild(new, old) |
Replaces old child node with new node. |
// Demonstrating DOM node creation, insertion, replacement, and removal
document.addEventListener("DOMContentLoaded", () => {
const container = document.createElement("div");
container.id = "card-list";
document.body.appendChild(container);
// 1. Creating and Appending Nodes
const item1 = document.createElement("div");
item1.className = "card-item";
item1.textContent = "Card #1";
container.appendChild(item1);
// 2. Prepending Node (Start of list)
const item0 = document.createElement("div");
item0.className = "card-item";
item0.textContent = "Card #0 (Prepended)";
container.prepend(item0);
// 3. Inserting Node Before Specific Reference
const item1_5 = document.createElement("div");
item1_5.textContent = "Card #0.5 (Inserted Before #1)";
container.insertBefore(item1_5, item1);
// 4. Replacing and Removing Nodes
const replacementNode = document.createElement("div");
replacementNode.textContent = "Replaced Card #0";
container.replaceChild(replacementNode, item0); // Replaces item0
// Direct Removal
item1_5.remove(); // Removes item1_5 directly
});
node.remove(): Modern browsers support calling element.remove() directly on the target node instead of needing parent.removeChild(element).insertAdjacentElement() for Precise Placement: insertAdjacentElement("beforebegin" | "afterbegin" | "beforeend" | "afterend", node) provides clean placement relative to any target element.Write a code snippet that creates a <button> with text "Submit", adds a click listener, and appends it to document.body.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.