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

160
Vistas
Remove grandchildren with certain class when grandparent is clicked

All these anchor tags have child paragraphs that have child spans.

When the user clicks any anchor, I want to remove the existing grandparent class from that particular anchor.

And if any anchor dosen't have the grandparent class, I want to remove the grandchildren element with grandchild class.

(There are some anchor that doesn't have any class or grandchild nested. I added an example element for that)[In case it matters]

const grandparents = document.querySelectorAll('a');

grandparents.forEach(elem => {
  elem.addEventListener('click',()=>{
   elem.classList.remove('grandparent');
  }
  if (!elem.classList.contains('grandparent')) {
    //remove element with the class of 'grandchild';
  }
})
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? </p>
</a>

almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

As the comments on your answer suggested, you need an event listener. In this event listener, you can use elem.querySelectorAll() to get all grandchildren with the class grandchild, then loop over the elements and remove them.

Also, you can replace the const grandparents = document.querySelectorAll('a') with const grandparents = document.querySelectorAll('.grandparent') to make sure this only happens on tags with the grandparent class.

const grandparents = document.querySelectorAll('.grandparent');
grandparents.forEach(elem => {
  elem.addEventListener('click', () => {
    elem.classList.remove('grandparent');
    const gc = elem.querySelectorAll('* > .grandchild');
    for (let i = 0; i < gc.length; i++) {
      gc[i].remove();
    }
  })
})
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? </p>
</a>

almost 4 years ago · Santiago Trujillo Denunciar

0

You almost have a solution written in your question.

if any anchor doesn't have the grandparent class, I want to remove the grandchildren element with grandchild class.

else

when the user clicks any anchor, I want to remove the existing grandparent class from that particular anchor.

if (!elem.classList.contains('grandparent')) {
    //remove element with the class of 'grandchild';
  } else {
    // remove element class grandparent
}

This snippet can be further simplified.

almost 4 years ago · Santiago Trujillo Denunciar

0

Here is a solution:

const grandparents = document.querySelectorAll('a');

grandparents.forEach(elem => {
  elem.addEventListener('click', () => {
  if (elem.classList.contains('grandparent'))
    elem.classList.remove('grandparent');
  else
    elem.querySelector('.grandchild')?.remove();
  });
});
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#" class="grandparent">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">&#8226;</span></p>
</a>
<a href="#">
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? </p>
</a>

almost 4 years ago · Santiago Trujillo 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