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

287
Views
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 answers
Answer question

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 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!