DOM Traversal

Once you've selected an Element, you can traverse to other elements in the tree using the following properties:

  • children

  • firstElementChild

  • lastElementChild

  • parentElement

  • nextElementSibling

  • previousElementSibling

.children is an HTMLCollection which is an array-like object. You can access its elements with bracket syntax [] and loop over it with for loops.

const list = document.getElementsById('my-list')
console.log(list.children[2])

for (let item of list.children) {
  console.log(item)
} 

Last updated