Selecting Elements

Learn how to select elements in JavaScript by index, class, or HTML tag.

Limitations of node-wise traversal

In the previous chapter, you saw how to navigate the DOM node structure of a web page beginning with the root node and using the childNodes property to move down levels in the structure of the page.

Suppose you want to select the title "Wonders from Antiquity" of our web page. Taking into account the text nodes between elements, this node is the second child node of the sixth child node of the body element. So you could write something like this.

Press the Run button to see the selected element on the console.

Get hands-on with 1200+ tech skills courses.