JS - pass this and event to event listener

const CTA = document.querySelector(".cta a");
const ALERT = document.querySelector("#booking-alert");

CTA.classList.remove("hide");
ALERT.classList.add("hide");

function reveal(e, current) {
    e.preventDefault();
    current.innerHTML == "Book Now!" ? current.innerHTML = "oops!" : current.innerHTML = "Book Now!";
    ALERT.classList.toggle("hide");
}

CTA.addEventListener("click", function(e) {
  reveal(e, this);
}, false);
CTA.addEventListener("click", function(){
  console.log("The button was clicked!")
}, false);

this: the object we just clicked on CTA

e: the triggered event is available at function (the call to the original function) function(e)

你可能感兴趣的:(WebDev)