0

I'm replacing inline event handling with EventListners in the JS file. I'm stuck on one of the form

onclick="funcMain(func1(document.getElementById('foo')), func2(document.getElementById('bar'))"

since I can't do

document.getElementById('offendingInlineElement').addEventListener.('onclick', funcMain('document.getElementById('foo')), func2(document.getElementById('bar'), false)

How can I pass arguments to the function funcMain?

Out of curiousity when a function is past like this is it known as a function pointer or is this something else?

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • why don't you just create a function that calls document.getElementById('foo') inside of it? Why do you need to pass this as an argument? – wheresmycookie Jul 24 '13 at 22:56
  • possible duplicate of [How can I pass a parameter to a callback?](http://stackoverflow.com/questions/1190642/how-can-i-pass-a-parameter-to-a-settimeout-callback) – Raymond Chen Jul 24 '13 at 22:58

1 Answers1

0

Use an anonymous function:

var inlineElement = document.getElementById('offendingInlineElement');

inlineElement.addEventListener("click", function(){
    //write any piece of code you desire here
    funcMain(document.getElementById('foo'));
}, false);
basilikum
  • 10,378
  • 5
  • 45
  • 58
  • I don't think this can be write because there's no mention of `bar` or `func1` or `func2`. Did you mean `function(){ funcMain(func1(document.getElementById('foo')), func2(document.getElementById('bar'))` – Celeritas Jul 24 '13 at 23:45
  • @Celeritas it was just an example. You call any function with any parameters you want inside of the anonymous function. – basilikum Jul 24 '13 at 23:56