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

380
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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