Chapter 46: DOM Add Nodes

XML DOM – Adding Nodes (also called “inserting” or “appending” new nodes).

I will explain everything as if I am your personal teacher sitting next to you — going very slowly, using simple analogies, drawing small trees on the whiteboard, showing before → after examples, warning about every possible trap, and giving you realistic, copy-paste-ready code you can run right now.

1. What does “adding nodes” actually mean?

Adding a node = creating a new node (element, text, attribute, comment, etc.) and attaching it to an existing place in the DOM tree.

The new node becomes part of the document and is visible when you serialize or display the XML.

Key facts you must remember from the very first second:

  • All new nodes are created by the same document that owns the tree → always use xmlDoc.createElement(), xmlDoc.createTextNode(), etc. → Never create nodes from a different document and try to insert them (you’ll get WrongDocumentError)
  • Creating a node does not automatically put it into the tree → You must explicitly attach it using one of the insertion methods
  • You can create → prepare → set attributes → add children → and only then attach it to the tree

Everyday analogy

Imagine the DOM is a big Lego city already built.

  • createElement(“house”) = you just bought a new Lego house kit
  • You can paint it, add windows, put furniture inside while it’s still in your hands (in memory)
  • Only when you decide to glue it to a street (attach to a parent) does it become part of the city

2. The four main ways to attach a new node

Method Syntax example Where the new node ends up When to use it Live effect
parent.appendChild(newNode) parent.appendChild(newElement) As the last child of parent Most common – add at the end Immediate
parent.insertBefore(newNode, referenceNode) parent.insertBefore(new, existingChild) Before the reference node Insert at specific position (before something) Immediate
parent.insertAdjacentElement() existing.insertAdjacentElement(“beforebegin”, new) Before / after / inside existing element Modern, very readable (2014+) Immediate
parent.prepend(newNode) parent.prepend(newElement) As the first child of parent Add at the beginning (ES2018+) Immediate

Modern recommendation (2025–2026)

Use appendChild() and insertBefore() for almost everything — they are the classic, most compatible, and safest methods. Use prepend() / append() / before() / after() only when you want very readable code and don’t care about older browsers.

3. Visual before / after – Adding different kinds of nodes

Before (original XML)

XML

After adding various nodes:

XML

4. Complete working example – Creating & adding different kinds of nodes

HTML

Summary – Quick reference for creating & adding nodes

Goal Creation method Attachment method Best practice / tip
Create new element doc.createElement(“discount”) parent.appendChild() Most common — create → set text/attributes → attach
Create text content doc.createTextNode(“text”) element.appendChild() Usually easier to use element.textContent = “…”
Create attribute element.setAttribute(“name”, “value”) — (no need to attach) Almost always use setAttribute instead of createAttribute
Create comment doc.createComment(“note”) parent.insertBefore() Usually for developer notes
Create CDATA section doc.createCDATASection(“raw text”) parent.appendChild() When you need to include raw HTML/JS inside XML
Insert before specific child parent.insertBefore(new, referenceChild) Very useful for maintaining order
Insert at beginning parent.prepend(new) or insertBefore(new, parent.firstChild) Modern prepend() is very readable

Would you like to continue with one of these next?

  • Inserting at very specific positions (before/after certain nodes, at index)
  • Creating complex subtrees (whole book with many children at once)
  • Moving existing nodes (cut from one place → paste in another)
  • Replacing while creating (create new → replace old)
  • Real-world patterns — adding new products, adding metadata, building dynamic catalogs
  • Debugging when new nodes “don’t appear” or XML becomes invalid

Just tell me which direction you want to explore next! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *