Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a class in javascript without targeting the parent element

I have a problem with this piece of code:

document.querySelector('.location__list-link[data-area-id="'+areaId+'"]').parentElement.classList.add("plop");

I actually want to add the "plop" class to ".location__list-link" but I have a second class behind it. This last class should not be taken into account.

The HTML is :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="location__wrapper">
  <div class="location__content">
    <ul class="location__list">
      <li class="location__list-item"><a data-area-id="1" class="location__list-link fond-bleu" href="https://www.antibes-juanlespins.com/">Antibes Juan-les-Pins</a></li>
      <li class="location__list-item" ><a data-area-id="22" class="location__list-link fond-vert" href="http://bezaudun.fr/">Bézaudun-les-Alpes</a></li>
      <li class="location__list-item" ><a data-area-id="3" class="location__list-link fond-bleu" href="https://www.biot.fr/">Biot</a></li>
      <li class="location__list-item"><a data-area-id="21" class="location__list-link fond-vert" href="https://bouyon.fr/">Bouyon</a></li>
    </ul>
  </div>
</div>

And I don’t know how to add a second class to my selection or what to do about .parentElement because when I delete it it doesn’t work at all.
Thank you in advance 🙂

>Solution :

Just remove parentElement and it works as one would expect. You can completely ignore the other class listed on the element:

const areaId=3
document.querySelector('.location__list-link[data-area-id="'+areaId+'"]').classList.add("plop");
.plop{ background-color:red}
<div class="location__wrapper">
  <div class="location__content">
    <ul class="location__list">
      <li class="location__list-item"><a data-area-id="1" class="location__list-link fond-bleu" href="https://www.antibes-juanlespins.com/">Antibes Juan-les-Pins</a></li>
      <li class="location__list-item"><a data-area-id="22" class="location__list-link fond-vert" href="http://bezaudun.fr/">Bézaudun-les-Alpes</a></li>
      <li class="location__list-item"><a data-area-id="3" class="location__list-link fond-bleu" href="https://www.biot.fr/">Biot</a></li>
      <li class="location__list-item"><a data-area-id="21" class="location__list-link fond-vert" href="https://bouyon.fr/">Bouyon</a></li>
    </ul>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading