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

164
Views
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 answers
Answer question

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 Report

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 Report

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