DOM: Navigate DOM Tree

By Xah Lee. Date: . Last updated: .

Here's how to navigate the DOM tree. That is, if you have a node, you can get its parent node, sibling node, or children nodes.

Get Parent Node

Get Previous/Next Sibling

document. getElementsByTagName("p")[0] = "green";

Get First/Last Child

Get Child Nodes

const x = document. body .children;
Array.from ( x ) .forEach (((x) => {"green" }));

Not Ignore Whitespace Nodes

The following may return Whitespace Nodes.

[see DOM: 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