libs:
- d3
- domtree
---
# Walking the DOM
DOM allows to do anything with elements and their contents, but first we need to reach the corresponding DOM object, get it into a variable, and then we are able to modify it.
All operations on DOM start with the `document` object. From it we can access any node.
[cut]
Here's a picture of links that allow to travel between DOM nodes:

Let's discuss them in more detail.
## On top: documentElement and body
The topmost tree nodes are available directly as `document` properties:
`` = `document.documentElement`
: The topmost document node is `document.documentElement`. That's DOM node of `` tag.
`
` = `document.body`
: Another widely used DOM node is the `` element -- `document.body`.
`` = `document.head`
: The `` tag is available as `document.head`.
````warn header="There's a catch: `document.body` can be `null`"
A script cannot access an element that doesn't exist at the moment of running.
In particular, if a script is inside ``, then `document.body` is unavailable, because the browser did not read it yet.
So, in the example below the first `alert` shows `null`:
```html run
```
````
```smart header="In the DOM world `null` means \"doesn't exist\""
In the DOM, the `null` value means "doesn't exist" or "no such node".
```
## Children: childNodes, firstChild, lastChild
There are two terms that we'll use from now on:
- **Child nodes (or children)** -- elements that are direct children. In other words, they are nested exactly in the given one. For instance, `` and `` are children of `` element.
- **Descendants** -- all elements that are nested in the given one, including children, their children and so on.
For instance, here `` has children `` and `
` (and few blank text nodes):
```html run
Begin
```
...And if we ask for all descendants of ``, then we get direct children ``, `
` and also more nested elements like `- ` (being a child of `
`) and `` (being a child of `- `) -- the whole subtree.
**The `childNodes` collection provides access to all child nodes, including text nodes.**
The example below shows children of `document.body`:
```html run
Begin
End
...more stuff...
```
Please note an interesting detail here. If we run the example above, the last element shown is `
````
## Siblings and the parent
*Siblings* are nodes that are children of the same parent. For instance, `` and `` are siblings:
- `` is said to be the "next" or "right" sibling of ``,
- `` is said to be the "previous" or "left" sibling of ``.
The parent is available as `parentNode`.
The next node in the same parent (next sibling) is `nextSibling`, and the previous one is `previousSibling`.
For instance:
```html run
```
## Element-only navigation
Navigation properties listed above refer to *all* nodes. For instance, in `childNodes` we can see both text nodes, element nodes, and even comment nodes if there exist.
But for many tasks we don't want text or comment nodes. We want to manipulate element nodes that represent tags and form the structure of the page.
So let's see more navigation links that only take *element nodes* into account:

The links are similar to those given above, just with `Element` word inside:
- `children` -- only those children that are element nodes.
- `firstElementChild`, `lastElementChild` -- first and last element children.
- `previousElementSibling`, `nextElementSibling` -- neighbour elements.
- `parentElement` -- parent element.
````smart header="Why `parentElement`? Can the parent be *not* an element?"
The `parentElement` property returns the "element" parent, while `parentNode` returns "any node" parent. These properties are usually the same: they both get the parent.
With the one exception of `document.documentElement`:
```js run
alert( document.documentElement.parentNode ); // document
alert( document.documentElement.parentElement ); // null
```
In other words, the `documentElement` (``) is the root node. Formally, it has `document` as its parent. But `document` is not an element node, so `parentNode` returns it and `parentElement` does not.
Sometimes that matters when we're walking over the chain of parents and call a method on each of them, but `document` doesn't have it, so we exclude it.
````
Let's modify one of examples above: replace `childNodes` with `children`. Now it shows only elements:
```html run
Begin
End
...
```
## More links: tables [#dom-navigation-tables]
Till now we described the basic navigation properties.
Certain types of DOM elements may provide additional properties, specific to their type, for convenience.
Tables are a great example and important particular case of that.
**``** element supports (in addition to the given above) these properties:
- `table.rows` -- the collection of `` elements of the table.
- `table.caption/tHead/tFoot` -- references to elements ``, ``, `
`.
- `table.tBodies` -- the collection of `` elements (can be many according to the standard).
**``, ``, ``** elements provide the `rows` property:
- `tbody.rows` -- the collection of `` inside.
**`
`:**
- `tr.cells` -- the collection of `` and ` | ` cells inside the given ` |
`.
- `tr.sectionRowIndex` -- the number of the given `
` inside the enclosing `/
`.
- `tr.rowIndex` -- the number of the `` in the table.
**`` and ` | `:**
- `td.cellIndex` -- the number of the cell inside the enclosing ` |
`.
An example of usage:
```html run height=100
```
The specification: [tabular data](https://html.spec.whatwg.org/multipage/tables.html).
There are also additional navigation properties for HTML forms. We'll look at them later when start working with forms.
# Summary
Given a DOM node, we can go to its immediate neighbours using navigation properties.
There are two main sets of them:
- For all nodes: `parentNode`, `childNodes`, `firstChild`, `lastChild`, `previousSibling`, `nextSibling`.
- For element nodes only: `parentElement`, `children`, `firstElementChild`, `lastElementChild`, `previousElementSibling`, `nextElementSibling`.
Some types of DOM elements, e.g. tables, provide additional properties and collections to access their content.