I have an event, and I want to add additional parameters to the named function. I tried following two things:
myDiv.addEventListener('click', evt.call(this, event, 'hello'));
And
myDiv.addEventListener('click', evt(event, 'hello'));
And the problem with both of them, is they get called right away, and don't get called when you click myDiv
, i.e. when it's supposed to get called.
How can I add additional parameters to the named function event?
console.clear();
var myDiv = document.getElementById('myDiv');
function evt(event, param1) {
console.log(event + ' and ' + param1)
}
myDiv.addEventListener('click', evt.call(this, event, 'hello'));
#myDiv {
width: 200px;
height: 200px;
background-color: green;
}
<div id="myDiv"></div>
See Question&Answers more detail:os