Chapter 42: DOM Change Nodes

XML DOM – Changing Node Values.

I will explain it as if I’m sitting next to you — going slowly, showing every step, using simple analogies, pointing out what beginners usually get wrong, explaining why certain things work or don’t work, and giving you many small-to-realistic examples that you can copy-paste and experiment with right now.

1. The most important mindset before we start

When we talk about changing node values in XML DOM, we mean:

Modifying the content or attributes of nodes that are already in the tree.

The DOM is live — every change you make is immediately reflected in the document object.

There are three main things you usually want to change:

  1. The text content inside an element
  2. The value of an attribute
  3. (Rarely) The text content of a text node directly

Each case uses slightly different properties.

2. Three main situations and the right tools for each

Situation You are working with … Recommended way to change it Example line of code Important notes / traps
Change the text inside an element <title>…</title> element.textContent = “new value” titleElement.textContent = “New Book Title” Safest & most common method
Change the direct text of a text node Text node itself textNode.nodeValue = “new text” titleElement.firstChild.nodeValue = “Updated” Rarely needed — textContent is usually better
Change an attribute value id=”old” → id=”new” element.setAttribute(“id”, “new”) or element.id = “new” price.setAttribute(“currency”, “USD”) Both ways work, setAttribute is safer
Remove an attribute Remove currency=”…” element.removeAttribute(“currency”) price.removeAttribute(“currency”) No value = attribute disappears

3. Visual example – before & after

Before (original XML)

XML

After we run the code below

XML

4. Complete working example – Changing different kinds of values

HTML

What happens when you click the buttons?

  1. Reset → restores original XML
  2. Change title → only text inside <title> changes
  3. Change author → updates author name
  4. Change price → updates both text and currency attribute
  5. Change ID → changes attribute on root element
  6. Add discount → creates new element and adds it

Summary – Quick reference for changing values

You want to change… Starting node type Recommended method Example code line Important notes
Text inside an element Element element.textContent = “new value” title.textContent = “New Title” Safest & most common
Direct text of a text node Text node textNode.nodeValue = “new text” title.firstChild.nodeValue = “Updated” Rarely needed
Attribute value Element element.setAttribute(“name”, “value”) price.setAttribute(“currency”, “USD”) Or element.currency = “USD” (for some attributes)
Remove attribute Element element.removeAttribute(“name”) price.removeAttribute(“currency”) Attribute disappears
Add new child element Any parent element parent.appendChild(newElement) book.appendChild(discountElement) Use createElement() first

Would you like to go deeper into one of these next?

  • How to change values in namespaced XML (very common in real data)
  • Creating & inserting elements at specific positions (before/after certain nodes)
  • Removing nodes safely while traversing
  • Updating attributes conditionally (only if they exist)
  • Real-world patterns — updating prices, changing statuses, adding new items in catalogs
  • Debugging when changes don’t appear or XML breaks

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 *