DEV Community

Cover image for JavaScript Tutorial Series: Traversing elements
The daily developer
The daily developer

Posted on • Updated on

JavaScript Tutorial Series: Traversing elements

Accessing and modifying elements located within the Document Object Model (DOM) tree is referred to as traversing elements in JavaScript. Each element of the document is represented as a node in the tree.
In this post we're going to learn how to get the parent, children and siblings of an element using specific attributes.

parent node

Using the parentNode property, allows you to determine the parent node of a specific node in the DOM tree.
a node.parentNode returns either null (if it doesn't exist) or the parent node of the specified node which is read-only.

<div id="container">
  <h1 class="title">Hello World</h1>
</div>
Enter fullscreen mode Exit fullscreen mode
const title = document.querySelector('h1');

const parent = title.parentNode;

console.log(parent);
console.log(parent.id);
Enter fullscreen mode Exit fullscreen mode

Image description

children elements

first child

Using the firstChild property, will allow you to retrieve the first child element of a given element.
The firstChild returns null if the parentElement has no children. A child node of any type, like an element node, a text node, or a comment node, is returned by the firstChild property.

<ol id="ordered-list">
  <li>eggs</li>
  <li>milk</li>
  <li>bread</li>
</ol>
Enter fullscreen mode Exit fullscreen mode
let content = document.getElementById('ordered-list');
console.log(content.firstChild.nodeName);
Enter fullscreen mode Exit fullscreen mode

first child property

In order to maintain the whitespace between the opening

    and
  • tags, a text node was inserted, which is why the Console shows #text.

    You can eliminate the whitespaces as follows in order to remove the #text node:

    <ol id="ordered-list"><li>eggs</li><li>milk</li><li>bread</li></ol>
    
    let content = document.getElementById('ordered-list');
    
    console.log(content.firstChild.nodeName);
    

    firstchild no whitespace

    The firstElementChild property allows you to get the first child using the Element node alone:

    <ol id="ordered-list">
      <li>eggs</li>
      <li>milk</li>
      <li>bread</li>
     </ol>
    
    let content = document.getElementById('ordered-list');
    console.log(content.firstElementChild);
    

    firstElementChild property

    get all the children

    You can use the children property to get a NodeList of a specified element's child elements:

    <ol id="ordered-list">
      <li>eggs</li>
      <li>milk</li>
      <li>bread</li>
    </ol>
    
    let content = document.getElementById('ordered-list');
    console.log(content.children);
    

    children property

    last child

    the lastChildproperty returns the node's last child. The child is typically an element node, a text node, or a comment node if its parent is an element. If there isn't any child nodes, it returns null.

    <ol id="ordered-list">
      <li>eggs</li>
      <li>milk</li>
      <li>bread</li>
    </ol>
    
    let content = document.getElementById('ordered-list');
    console.log(content.lastElementChild);
    

    lastchild property

    siblings

    Next sibling

    You can use the nextElementSibling attribute to find an element's next sibling.
    If the referenced element is the first in the list, the nextElementSibling property returns null.

    <ol id="ordered-list">
      <li>eggs</li>
      <li id="current">milk</li>
      <li>Lettuce</li>
      <li>bread</li>
    </ol>
    
    let current = document.getElementById('current');
    console.log(current.nextElementSibling);
    

    next sibling property

    previous sibling

    The previousElementSibling attribute is used to retrieve an element's previous siblings.
    If the current element is the first in the list, the previousElementSibling property returns null.

    <ol id="ordered-list">
      <li>eggs</li>
      <li id="current">milk</li>
      <li>Lettuce</li>
      <li>bread</li>
    </ol>
    
    let current = document.getElementById('current');
    console.log(current.previousElementSibling);
    

    previous sibling

Top comments (0)