Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select only direct children of parent div, not all sub divs

So I’m trying to select 3 divs in my parent element. With my current code the result is I get 162 nodeLists back, instead of just the 3 main divs in that parent element.

The page code looks like this (simplified):

var parent = document.querySelector('.parent');

var wantedChildren = parent.querySelectorAll('div');

console.log(wantedChildren);
<div class="parent">
  <div class="wantedChild">
    <div class="unwantedChild">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
    <div class="unwantedChild"></div>
  </div>
  <div class="wantedChild">
    <div class="unwantedChild"></div>
    <div class="unwantedChild"></div>
  </div>
  <div class="wantedChild">
    <div class="unwantedChild"></div>
    <div class="unwantedChild"></div>
  </div>
</div>

So the divs are just examples. The entire code on the page is way bigger.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I just want those 3 divs.

Does anybody know how to do this?

>Solution :

var wantedChildren = parent.children;

https://developer.mozilla.org/en-US/docs/Web/API/Element/children

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading