-1

I have a button which on click is supposed to execute the addEventListener function. On the callback function part I directly use a console.log.

<button id="btnTrans">Translate</button>
var buttonTranslate = document.querySelector("#btnTrans");
buttonTranslate.addEventListener("click", console.log("clicked"));

as soon as the DOM loads, "clicked" appears on the console. Shouldn't it wait for the event to happen first?

code-mon
  • 71
  • 2
  • 10

1 Answers1

1

The console.log is immediately executed because it is not wrapped in a function. Wrap it in a function:

buttonTranslate.addEventListener("click", () => { console.log("clicked") });
MrCode
  • 63,975
  • 10
  • 90
  • 112