views:

1269

answers:

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

+3  A: 

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
That works like a charm.
rams
Thanks for the quick response. I keep forgetting the hidden powers of JavaScript.
rams
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
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
Can you point me to an article/blog post that can help me better understand closures and their implications? - Thanks
rams
Well, try reading this: http://stackoverflow.com/questions/111102/how-does-a-javascript-closure-work
Jason Bunting