mardi 10 août 2021

If link exist on the page, add class on click, javascript

At first, I check if button with class prices__link exist on the page, and then I need to open modal block on click. For one button it works, but not for second one. Could you please help what is missing?

HTML:

<a class="prices__link" href="">Show more</a>
<br>
<a class="prices__link" href="">Button 2</a>
<div class="popup-business popup-business--hide">Modal</div>

JavaScript:

var popupPrices = document.querySelector('.popup-business');
var buttonPopupPricesOpen = document.querySelectorAll('.prices__link');
var buttonPopupPricesItem = document.querySelector('.prices__link');

if (buttonPopupPricesOpen.length !== 1) {

  buttonPopupPricesItem.addEventListener('click', function(evt) {
    evt.preventDefault();
    popupPrices.classList.toggle('popup-business--hide');
  });

  }

Thanks

Aucun commentaire:

Enregistrer un commentaire