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

150
Views
Count parent child child nodes

So I want to know how do I get count of parent child child childs for example:

const parent = document.querySelectorAll('.parent');
parent.forEach(el => {
  const ul = el.querySelector('.child3-child');
  const div = document.createElement('div');
  div.textContent = ul.childNodes.length;
  el.append(div);
});
<div class="parent">
  <div class="child1">
   text
  </div>
  <div class="child2">
   text
  </div>
  <div class="child3">
    <ul class="child3-child">
     <li>
      some text...
     </li>
    <ul>
  </div>
</div>

And now I want count how many <ul class="child3-child"> has child elements in this case it has only 1 li.

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

0

  • Use children instead of childNodes. The former includes HTML Elements while the latter includes text nodes.
  • Close your </ul> tag properly or else the borwser will think it's opening a nested element.

const parent = document.querySelectorAll('.parent');
parent.forEach(el => {
  const ul = el.querySelector('.child3-child');
  const div = document.createElement('div');
  div.textContent = ul.children.length;
  el.append(div);
});
<div class="parent">
  <div class="child1">
   text
  </div>
  <div class="child2">
   text
  </div>
  <div class="child3">
    <ul class="child3-child">
     <li>
      some text...
     </li>
    </ul> <!--- This wasn't properly closed -->
  </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!