Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a function as parameter in jQuery?

I would like to pass to a jQuery function a regular function, instead of the usual anonymous function, but I'm not sure how such a thing could be done.

Instead of this:

function setVersion(feature) {
      $.post("some.php", { abc:"abc" },
      function(data){
         // do something here
      }, "json");
}

I would like to do this:

function foo(data){
   // do something here
}

function setVersion(feature) {
      $.post("some.php", { abc:"abc" }, foo, "json");
}

Thank you.

like image 299
thedp Avatar asked Apr 17 '10 20:04

thedp


1 Answers

Yeah, already works. But you want it probably look like this:

function setVersion(feature, myFunction) {
      $.post("some.php", { abc:"abc" }, myFunction, "json");
}
setVersion(blah, foo);
like image 162
elias Avatar answered Sep 28 '22 18:09

elias