0
document.querySelector(DOM.refreshRem).addEventListener('click', function(param));

This is the line I'm having trouble with, I want to connect the function but without the (), so it isn't invoked right away, but the function accepts a parameter, so how do I go around it, my first idea would be to just wrap it in another function, but is there an easier way, this seems like too much nesting

sljafna
  • 37
  • 4
  • 2
    Just go with your first idea, that's the simplest way. You can of course choose [the long path](https://stackoverflow.com/a/39354642/1169519) too. – Teemu Apr 20 '20 at 14:01
  • `('click', function(event) { yourFunction(event, 'foo'): })` or bind – epascarello Apr 20 '20 at 14:01

1 Answers1

0

So use a function or use bind

function test1(event, param) {
  console.log(param)
}

function test2(param, event) {
  console.log(param)
}

const btn1 = document.querySelector('.test1')

btn1.addEventListener('click', function(event) {
  test1(event, 'foo')
});

const btn2 = document.querySelector('.test2')
btn2.addEventListener('click', test2.bind(btn2, 'bar'));
<button type="button" class="test1">click 1</button>

<button type="button" class="test2">click 2</button>
epascarello
  • 204,599
  • 20
  • 195
  • 236