Traversing the DOM

Traversing the DOM is the term used to describe the act of finding and selecting nodes within a web page.

 

Various properties enable the selection of nodes that can be used to move within a document.

 

Node.childNodes Returns an array of all child nodes of the specified parent node
Node.firstChild Equivalent to the first item in the ‘childNodes’ array (‘Element.childNodes[0]‘)
Node.lastChild Equivalent to the last item in the ‘childNodes’ array (‘Element.childNodes[Element.childNodes.length-1]‘)
Node.parentNode Provides access to parent node of specified node.
Only one parent node.
Grandparent would be accessed by ‘Node.parentNode.parentNode’
Node.nextSibling Provides access to next node on the same level within the DOM tree.
Node.previousSibling Provides access to last node on the same level within the DOM tree.

Leave a Reply