Write your HTML, chain .parentElement/.nextSibling/.closest() on its real DOM, and compare its tree with what DevTools shows. An interactive DOM Traversal…
A classic beginner bug: reach for childNodes/firstChild and get a text node made of the spaces between tags. Knowing when to use the Element family or the Node family, and when closest()/querySelector() is enough, makes your traversal correct and robust to markup changes.
The enters and spaces in your HTML become nodes too
firstChild gives #text, not an element The enter + spaces between tags are a text node, and DevTools does not show it. Use firstElementChild (or children[0]) for the first element..parentElement.children[2] breaks as soon as the markup changes: one hop gets undefined/null, the next one throws a TypeError. Use closest() to go up and querySelector() to go down: both search by selector, not by position.children as an array It is a live HTMLCollection with no map. Wrap it first: [...el.children] or Array.from(el.children).table.firstElementChild gives tbody, though you never wrote <tbody> The HTML parser adds a <tbody> on its own for a <tr> placed straight in a <table>. Traversal walks the parsed DOM, not your HTML text. Use table.rows or table.querySelector('tr').ul.menu, press .firstChild You get #text "↵␣␣␣␣": the enter + spaces before the first <li>. Not an element..firstElementChild or .nextElementSibling The Why names the nodes it jumped over: #text, sometimes a #comment. The Element family only stops on elements..firstChild again Without the enters + spaces, firstChild gets the <li> straight away. The whitespace makes the difference, not the property..parentElement until null, then one more hop A real TypeError from the browser. That is how a long chain breaks when the markup changes.