Walking The DOM
The DOM allows us to do anything with elements and their contents, but first we need to reach the corresponding DOM object. All operations on the DOM start with the document object - the main entry point
.

On top: documentElement and body
<html>
= document.documentElement
<body>
= document.body
<head>
= document.head
</head>
</body>
</html>
document.bodycan benull
Children: childNodes, firstChild, lastChild
There are 2 terms that we'll use from now on:
Child nodes (or children) - elements that are direct children. For instance,
<head>and<body>are children of<html>element.Descendants - all elements that are nested in the given one, including children, their children, and so on.
childNodes - lists all child nodes, including text nodes
firstChild - gives fast access to the first children
lastChild - gives fast access to the last children
DOM collections
As we can see, childNodes looks like an array. But actually it's not an array, but rather a collection - a special array - like iterable object.
There are 2 important consequences:
We can use
for..ofto iterate over it:for (let node of document.body.childNodes) { console.log(node); }Array methods won't work, because it's not an array:
alert(document.body.childNodes.filter); // undefined (there's no filter method) alert(Array.from(document.body.childNodes).filter); // functionDOM collections are read-only.
DOM collections are live (current state of DOM).
Don't use
for..into loop over collections.
Siblings and the parent
Siblings are nodes that are children of the same parent. For instance, <head> and <body> are siblings:
<body>is said to be thenext
orright
sibling of<head><head>is said to be theprevious
orleft
sibling of<body>The next sibling is innextSiblingproperty, and the previous one - inpreviousSibling. The parent is available asparentNode.
alert(document.body.parentNode === document.documentElement); // true
alert(document.head.nextSibling); // HTMLBodyElement
alert(document.body.previousSibling); // HTMLHeadElement
Element-only navigation
Navigation properties listed above refer to all nodes. But for many tasks we don't want text or comment nodes. We want to manipulate element nodes that represents 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- neighbor elements.parentElement- parent element.
More links: tables
The <table> element supports (in addition to the given above) these properties:
table.rows- the collection of<tr>elements of the table.table.caption/tHead/tFoot- references to elements<caption>,<thead>,<tfoot>table.tBodies- the collection of<tbody>elements (can be many according to the standard, but there will alwas be at lest one - even if it is not in the source HTML, the browser will put it in the DOM).
<thead>, <tfoot>, <tbody> elements provide the rows property:
tbody.rows- the collection of<tr>inside.
<tr>:
tr.cells- the collection of<td>and<th>cells inside the given<tr>tr.sectionRowIndex- the position (index) of the given<tr>inside the enclosing<thead>/<tbody>/<tfoot>.tr.rowIndex- the number of the<tr>in the table as a whole (including all table rows).
<td> and <th>:
<td.cellIndex- the number of the cell inside the enclosing<tr>.
Searching: getElement*, querySelector*
document.getElementById- If an element has theidattribute, we can get the element no matter where it is.elem.querySelectorAll(css)- Returns all elements insideelemmatching the given CSS selector.elem.querySelector(css)- Returns the first element for the given CSS selector.elem.matches(css)- Checks if anelemmatches the given CSS-selector. It returnsboolean.elem.closest(css)- Looks for the nearest ancestor that matches the CSS-selector. Theelemitself is also included in the search.elem.getElementsByTagName(tag)- Looks for elements with the given tag an returns the collection of them. Thetagparameter can also be a star"*"forany tags
.elem.getElementsByClassName(className)- Returns elements that have the given CSS class.document.getElementsByName(name)returns elements with the givennameattribute, document-wide. Very rarely used.