Home > Enterprise >  anyone able to translate this jquery code to Vanilla Javascript?
anyone able to translate this jquery code to Vanilla Javascript?

Time:11-08

$('.flip-button').each(function() {
    $(this).click(function() {
      $(this).closest('.flip-card-inners').toggleClass('flip-card-active')
    })
  })

I wasn't able to translate this in vanilla js because I encountered several problems with get element by class

CodePudding user response:

document.querySelectorAll(".flip_button").forEach(element => {
    element.onclick = () => {
        element.closest('.flip-card-inners').classList.toggle("flip-card-active");
    }
});

CodePudding user response:

document
.querySelectorAll('.flip-button')
.forEach((element)=> {
  element.addEventListener("click", function(e) {
  // Do your action
  }, false);
})

  • Related