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

291
Vistas
Querying a specific child in a recursive DOM structure

I have a DOM structure like this:

<div class="thing">
  <div class="title"></div>
  <div class="content">
    <div class="thing">
      <div class="title"></div>
      <div class="content">
        <!-- More .thing may be here-->
      </div>
    </div>
  </div>
</div>

In this example, I have a class .thing that has a .title and .content. The contents of .content may be another .thing. The issue I'm having is that given an arbitrary .thing, I want to be able to query its .content and .title without querying anything inside of .content.

One way to do this would be to query the child directly, e.g.

const thing = ... // get some .thing
const title = thing.querySelector(':scope > .title')

The issue with this is that I want to be able to insert an arbitrary number of elements in between .thing and .title. One idea I had was to use the :not pseudo-class:

const title = thing.querySelector('.title:not(.content *)'). 

The theory behind this is that I want a title that isn't a child of content. This works for the top-level title, but doesn't work for nested things. What's the best way to query for this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

give a data-attribute to the content you specifically want to grab.

Example:

const div = document.querySelector("[data-getter='title']");
console.log(div);
[data-getter="title"] {
  background-color: orange;
}
<div>
  <div data-getter="title">
     <p>random text</p>
  </div>
   <div>2nd text</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