Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
How can I get the path to a specific DOM element iterating a HTML collection?

This is the issue: I want to have nested divs with paragraphs inside with different texts.

I want to be able to get the paragraph that contains certain word, for example "mate" I did the below HTML structure trying to obtain an HTML collection and iterate it, and then using javascript, try to use the includes method to get the paragraph than contains that word, and finally, try to find a way to get the full path from the uppermost div to this p.

<div class="grandpa">
    <div class="parent1">
      <div class="son1">
        <p>I like oranges</p>
      </div>
      <div class="son2">
        <p>yeeeey</p>
        <p>wohoo it's saturday</p>
      </div>
      <div class="son3"></div>
    </div>
    <div class="parent2"></div>
    <div class="parent3">
      <div class="son1">
        <p>your team mate has been killed!</p>
        <p>I should stop playing COD</p>
      </div>
      <div class="son2"></div>
    </div>
  </div>

I actually don't know how to achieve it, but at least I wanted to get an HTML collection to iterate, but I'm not being able to get it.... When I use this:

const nodes = document.querySelector('.grandpa');
console.log(typeof nodes);

I don't get an HTML collection, instead if I console.log typeof nodes variable it says it is an object..

How can I iterate this DOM tree, capture the element that contais the word "mate", and obtain (this is what I really want to achieve) the path to it?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can loop through every element, remove all children elements, then check whether the textContent includes the string you are looking for:

const allElements = document.body.querySelectorAll('*');
const lookFor = "mate";
var elem;
for (let i = 0; i < allElements.length; i++) {
  const cur = allElements[i].cloneNode(true); //doesn't mess up the original element when removing children
  while (cur.lastElementChild) {
    cur.removeChild(cur.lastElementChild);
  }
  if (cur.textContent.includes(lookFor)) {
    elem = cur;
    break;
  }
}

console.log(elem);
<div class="grandpa">
  <div class="parent1">
    <div class="son1">
      <p>I like oranges</p>
    </div>
    <div class="son2">
      <p>yeeeey</p>
      <p>wohoo it's saturday</p>
    </div>
    <div class="son3"></div>
  </div>
  <div class="parent2"></div>
  <div class="parent3">
    <div class="son1">
      <p>your team mate has been killed!</p>
      <p>I should stop playing COD</p>
    </div>
    <div class="son2"></div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!