📝 DOM Text – Handle Text Nodes Inside XML and HTML Elements
🧲 Introduction – Why Learn About DOM Text Nodes?
In both XML and HTML, any text inside an element is represented in the DOM as a Text node. These nodes are distinct from elements and attributes—they specifically contain text content only. Understanding how DOM Text
nodes work is essential for reading, modifying, and sanitizing document content, especially when navigating or manipulating documents dynamically with JavaScript or XML DOM parsers.
🎯 In this guide, you’ll learn:
- What a
Text
node is and how it fits into the DOM - How to access and manipulate text content
- Differences between
textContent
,nodeValue
, andinnerText
- Use cases and best practices for XML and HTML contexts
📘 What Is a DOM Text Node?
A DOM Text node is a nodeType = 3 and contains only character data (no tags or attributes). It lives inside elements and represents the actual text that users see or parse.
Example XML:
<title>Learn XML</title>
<title>
is anElement
node"Learn XML"
is aText
node inside the element
🔍 Accessing Text Nodes in JavaScript
const title = document.getElementsByTagName("title")[0];
const textNode = title.firstChild;
console.log(textNode.nodeType); // 3
console.log(textNode.nodeValue); // "Learn XML"
✅ You can also modify it:
textNode.nodeValue = "XML Mastery";
🔁 Alternatives: textContent
vs innerText
Property | Description |
---|---|
textContent | Gets/sets all text inside an element (including hidden) |
innerText | Like textContent , but respects CSS visibility (HTML only) |
nodeValue | Works on the text node directly |
🧠 Common Use Cases
- 📄 Reading values from XML elements (
book.title.textContent
) - 🧼 Sanitizing user inputs (by avoiding
innerHTML
) - 🧾 Generating dynamic reports or tables
- 📥 Parsing API responses formatted as XML
🧾 Example – Modify Text Node Content
const price = document.querySelector("price").firstChild;
price.nodeValue = "499.00";
✅ Or, simpler:
document.querySelector("price").textContent = "499.00";
⚠️ Whitespace and Text Nodes
In XML and HTML, text nodes often include whitespace like \n
or between elements.
Example:
<book>
<title>Learn XML</title>
<author>John</author>
</book>
book.childNodes
might return:
Text
node (whitespace\n
)Element
node<title>
Text
node (whitespace\n
)Element
node<author>
✅ Always filter with nodeType === 3
or use .children
for only elements.
✅ Best Practices with Text Nodes
- ✔️ Use
textContent
for simple text handling - ✔️ Use
.firstChild.nodeValue
when working directly with the text node - ✔️ Always trim whitespace if text nodes might include line breaks
- ❌ Don’t use
innerHTML
for raw text—it risks security (XSS) - ❌ Avoid assuming
.childNodes[0]
is always text—checknodeType
📌 Summary – Recap & Next Steps
Text nodes represent the character content inside elements, and are crucial to navigating and modifying XML or HTML documents. Whether you’re reading titles or updating labels, Text
nodes give you direct access to the visible text.
🔍 Key Takeaways:
- Text nodes are
nodeType = 3
and contain only character data - Use
textContent
ornodeValue
to read/write values - Whitespace text nodes are common—filter carefully when traversing
⚙️ Real-world relevance: Used in XML parsing, web app interfaces, form labels, content rendering, and digital publishing.
❓ FAQs – DOM Text
❓ Is text a child node of elements?
✅ Yes. Text nodes are direct children of their containing elements.
❓ What’s the best way to read inner text?
✅ Use .textContent
for all text; use .innerText
if you want to respect visibility (HTML only).
❓ How do I create a text node manually?
✅ Use document.createTextNode("Hello World")
.
❓ Why does .childNodes
include text nodes?
✅ Because text between tags (even whitespace) is stored as text nodes.
❓ Can I remove a text node?
✅ Yes. Use parent.removeChild(textNode)
if you have the reference.
Share Now :