Get Element Ancestors

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will be exploring how to use JavaScript to retrieve all the ancestors of an element from the document root to the given element. This will involve using Node.parentNode and a while loop to navigate up the ancestor tree and Array.prototype.unshift() to add each new ancestor to the start of an array. By the end of the lab, you should have a solid understanding of how to retrieve element ancestors using JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/DOMManipulationGroup(["`DOM Manipulation`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/DOMManipulationGroup -.-> javascript/dom_select("`DOM Selection`") javascript/DOMManipulationGroup -.-> javascript/dom_traverse("`DOM Traversal`") javascript/ToolsandEnvironmentGroup -.-> javascript/bom("`Browser Object Model`") subgraph Lab Skills javascript/variables -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/data_types -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/arith_ops -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/comp_ops -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/loops -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/dom_select -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/dom_traverse -.-> lab-28350{{"`Get Element Ancestors`"}} javascript/bom -.-> lab-28350{{"`Get Element Ancestors`"}} end

Retrieve Element Ancestors

To retrieve the ancestors of an element from the document root to the specified element, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Node.parentNode and a while loop to move up the ancestor tree of the element.
  3. Use Array.prototype.unshift() to add each new ancestor to the start of the array.

Here's a sample code that implements the above steps:

const getAncestors = (el) => {
  let ancestors = [];
  while (el) {
    ancestors.unshift(el);
    el = el.parentNode;
  }
  return ancestors;
};

To retrieve the ancestors of a specific element, use the querySelector() method to select the element and pass it as an argument to the getAncestors() function. For example:

getAncestors(document.querySelector("nav"));
// [document, html, body, header, nav]

This will return an array of all ancestors of the specified element in the order from the document root to the element itself.

Summary

Congratulations! You have completed the Get Element Ancestors lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like