Z Ig NQQ
Z Ig NQQ
The method appendChild() adds a new node as the last child of the specified
parent.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Append Child Example</title>
</head>
<body>
<div id="div1">
<p>Existing Paragraph</p>
</div>
<script>
var parentDiv = document.getElementById("div1");
var newParagraph = document.createElement("p");
var t = document.createTextNode("Hello world!");
newParagraph.appendChild(t);
parentDiv.appendChild(newParagraph);
</script>
</body>
</html>
Result: A new paragraph with "Hello world!" is added at the end of the
<div>.
Since there is no insertAfter(), you can achieve this by targeting the next
sibling of the node and inserting the new element before it.
4. Removing a Node
Key Points
These methods give you complete control over how nodes are
manipulated and displayed dynamically in your web page.