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

376
Views
querySelectorAll exclude all children

I'm using querySelectorAll and want to exclude all children of a number of elements. Trying this does not work.

document.querySelectorAll(".h:not(.p)").forEach(function(e){
  e.style.color = "red";
});
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

How can I achieve only selecting parent elements? Does such a selector exist?

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

0

If you give a parent a color, that will be inherited by the children as well, by default. Your code is selecting only the parents, the .hs, as you want - the problem is that the children inherit that style as well.

One option is to iterate over the children or descendant <p>s as well, and change their colors to what you want, so they don't inherit from the parent.

document.querySelectorAll(".h").forEach(function(e){
  e.style.color = "red";
});
document.querySelectorAll(".h .p").forEach(function(e){
  e.style.color = "initial";
});
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

A nicer looking option would be to give the text to be colored its own element, and then select those elements only:

document.querySelectorAll(".h span").forEach(function(e){
  e.style.color = "red";
});
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

This is only selecting the .h elements:

document.querySelectorAll(".h")

But what are you doing with those elements? You're setting the text color to red:

e.style.color = "red";

So all text within those elements will be red, unless styled otherwise. If the .p elements should be something else, style them as such:

document.querySelectorAll(".h").forEach(function(e){
  e.style.color = "red";
});
.p {
  color: black;
}
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

Alternatively you might consider re-structuring your HTML. For example:

<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>

Then you can specifically target the content you want without having to find ways to "exclude" other content.

about 4 years ago · Juan Pablo Isaza Report

0

The color is being inherited in the paragraph

document.querySelectorAll(".h:not(.p)").forEach(function(e){
  e.style.color = "red";
  e.querySelectorAll(".p").forEach(function(elem) {
    elem.style.color = "black";
  });
});
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!