Reputation: 345
If I am calling a function in addEventListener like this:
document.querySelector('.elem').addEventListener('click', expand);
The expand function takes arguments. How can I pass those arguments to it?
I've tried
document.querySelector('.elem').addEventListener('click', expand(arg1, arg2, arg3));
But that doesn't work.
Upvotes: 4
Views: 179
Reputation: 318162
With an anonymous function call
document.querySelector('.elem').addEventListener('click', function() {
expand(arg1, arg2, arg3)
}, false);
if you need the value of this
to be the element, you could use bind
, apply
or call
document.querySelector('.elem').addEventListener('click', function() {
expand.apply(this, [arg1, arg2, arg3]);
}, false);
Upvotes: 3