jeudi 1 avril 2021

If class doesn't exist do this else ignore

Hi what I'm trying to do in Javascript is to find if a class '.promo' exists in the '.productDetails' div, if it does then no action needs to be taken, but if it doesn't exist then i want it to print the copy in the span tags. I know this can be done in jQuery but looking to do this in plain Javascript instead.

var elem = document.querySelector('.product-details');
if(!elem.classlist.contains('.promo')){
   document.querySelector('.title').innerHTML += "<span class="promo">Add Promo Banner</span>";
 }
.product-details {border: 1px black solid; margin: 10px 0}
<div class="product-details">
  <div class="title">Title</div>
  <div class="promo">Promo 20% off</div>
</div>

<div class="product-details">
  <div class="title">Title</div>
</div>

Aucun commentaire:

Enregistrer un commentaire