DOM: Whitespace Nodes

By Xah Lee. Date: . Last updated: .

Whitespace characters in HTML source code between elements are considered a node too. (whitespace includes space, newline, tab)

For example, if you have:

<ul id="x1">

And you count the number of the ul's children nodes, you get 5.

document. getElementById("x1").childNodes.length // prints 5

because the newline characters in between tags are also considered nodes.

Here's another example, you can try in browser console:

// get the next node after the first paragraph
let xx = document. getElementsByTagName("p")[0].nextSibling;

// 3 means text node (whitespace node)

// if whitespace node, then result is space or newline or tab, etc.

[see DOM: Node Type, Node Name, Node Value]

How to Skip Whitespace Nodes

Use the following methods that skip the whitespace nodes.

[see DOM: Navigate DOM Tree]


whitespace nodes 2019-05-26 d5bqv
whitespace nodes []

DOM How-To

Web Scripting Overview

HTML Input

Web Scripting Examples

Web Scripting Misc



Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials