Search code examples
javascriptcallback

How can I set up a callback to be invoked with extra custom arguments?


I wanted to my code a bit cleaner so I wanted to put a very long function in it's own method.

Right now I have

$('#Id').submit(function()
{
   // lots of code here
});

now I wanted to break it up like

$('#Id').submit(MyFunction);

function MyFunction()
{
   // code now here.
}

but I need to pass in some parms into MyFunction. So I tried

$('#Id').submit(MyFunction(param1, param2));

function MyFunction(param1, param2)
{
   // code now here.
}

But when I do this nothing works. Firebug shows me some huge ass error about F not being defined or something. So I am not sure what I need to do to make it work.


Solution

  • You just need to write:

    $('#Id').submit(function()
    {
       MyFunction(param1, param2);
    });