1

I am trying to use the $addHandler function to add a handler to a text box's click event

var o=$get('myTextBox');
var f = Type.parse('funcWithArgs');
$addHandler(o, 'click', f);

However I need to pass parameters to the called function. How do you do that?

TIA

rams
  • 6,381
  • 8
  • 46
  • 65

1 Answers1

3

Wrap your function with an anonymous function (aka lambda):

$addHandler(o, 'click', function() { f(my, arguments, go, here); });

Alternative solution:

If you had a function that created partials, you could do that as well - I use a toolkit that provides for that, and this is how it would be done:

$addHandler(o, 'click', partial(f, my, arguments, go, here));

I don't know (and actually doubt) that Microsoft's framework provides for that, but you could look into writing your own partial function.

Jason Bunting
  • 58,249
  • 14
  • 102
  • 93
  • Thanks for the quick response. I keep forgetting the hidden powers of JavaScript. – rams Sep 26 '08 at 19:01
  • No problem - I have been earnestly hacking JavaScript for about 3.5 years now and love it to death. I am always amazed at how powerful it is and love showing people that think it is a joke just how much you can do with it. Not that it is perfect! – Jason Bunting Sep 26 '08 at 19:08
  • Oh, and just make sure you are careful about closures when you set that up...it can come back to bite you unless you understand what those are... – Jason Bunting Sep 26 '08 at 19:12
  • Can you point me to an article/blog post that can help me better understand closures and their implications? - Thanks – rams Sep 26 '08 at 19:15
  • Well, try reading this: http://stackoverflow.com/questions/111102/how-does-a-javascript-closure-work – Jason Bunting Sep 26 '08 at 19:32