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">•</span></p>
</a>
<a href="#" class="grandparent">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">•</span></p>
</a>
<a href="#" class="grandparent">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? <span class="grandchild">•</span></p>
</a>
<a href="#">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Delectus, dolorum? </p>
</a>