-1

I am trying to convert the following jQuery function to Javascript (to use in a Vue.js component).

$(".price-block a").on("click", function() {
  $(".price-block")
    .find(".dropdown-container")
    .toggleClass("is-open");
});

This is what I have so far (not working):

document.getElementsByClassname(".price-block a").addEventListener("click", displayOptions);

function displayOptions() {
  document.querySelector("price-block a").querySelectorAll('.dropdown-container').toggle('is-open');
}

Anyone able to help? Or is there an online converter?

Derek C
  • 323
  • 3
  • 13

1 Answers1

0

Here I assume more than one dropdown per form.

[...document.querySelectorAll(".price-block a")].forEach(el => el.addEventListener("click", displayOptions));

function displayOptions() {
  [...this.closest("form").querySelectorAll('.dropdown-container')].forEach(el => el.classList.toggle('is-open') );
}

If only one per form, then use this

  this.closest("form").querySelector('.dropdown-container').classList.toggle('is-open');

Delegated from each form:

[...document.querySelectorAll(".price-block")].forEach(el => el.addEventListener("click", displayOptions));

function displayOptions(e) {
  if (e.target.tagName === "A") { 
    [...e.currentTarget.querySelectorAll('.dropdown-container')] 
      .forEach(el => el.classList.toggle('is-open') );
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236