Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda