0

When adding the eventlistener ("click", myFunction) how can I pass in a parameter to myFunction without invoking it? Like... ("click", myFunction(arrayOne)). The button is triggering a function that uses arrays. I want the function to use a specific array based on a specific button.

   var btnOne = document.getElementById("btnOne);
   btnOne.addEventListener("click", myFunction);
Kolu99
  • 5
  • 2

2 Answers2

1

You can use arrow function to do it. It'll help you by not creating a new lexical scope, so no need to worry about mishandling this.

var btnOne = document.getElementById("btnOne");
btnOne.addEventListener("click", () => { myFunction(arg1, arg2...) });
Kanishk Anand
  • 1,686
  • 1
  • 7
  • 16
0

try this:

var btnOne = document.getElementById("btnOne);
btnOne.addEventListener("click", () => myFunction(params));
Jafar Jabr
  • 642
  • 5
  • 11