Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda