codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS DOM navigation

JS DOM navigation

Move through the tree.

Use parentElement, children, firstElementChild, nextElementSibling, closest() and querySelectorAll.

Example code

<ul id='l'><li>One</li><li id='t'>Two</li><li>Three</li></ul><p id='o'></p>
<script>
o.textContent='Next: '+t.nextElementSibling.textContent+', parent: '+t.parentElement.id+', items: '+l.children.length;
</script>

Run this code in the online editor

← JS versionsJS window API →