Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send a jquery callback function inside a variable

Tags:

jquery

I have this jquery function

    function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        $("#widget_accordion").accordion({fillSpace: true});
    });
}

it's working ok when I do:

 example('http://example.com/', "#divid", callback);

but I want to send the callback function inside the (callback) variable i.e: instead of butting the $("#widget_accordion").accordion({fillSpace: true}); inside the callback function I want to send it:

 example('http://example.com/', "#divid", '$("#widget_accordion").accordion({fillSpace: true});');

and then the function must be something like:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback;
});

but that's not working

Thanks in advance for your help

like image 262
Bassel Safadi Avatar asked Jul 20 '09 12:07

Bassel Safadi


3 Answers

To pass the callback around, the variable needs to be of type function. Any of these should work:

function example(file, targetwidget, callback) {
  $(targetwidget).load(file, {limit:25}, callback);
}

// Call it by providing the function parameter via inline anonymous function:
example('http://example.com/', "#divid", function() {
  $("#widget_accordion").accordion({fillSpace: true});
});

// Or, declare a function variable and pass that in:
var widgetCallback = function() {
  $("#widget_accordion").accordion({fillSpace: true});
};

example('http://example.com/', "#divid", widgetCallback);

// Or, declare the function normally and pass that in:
function widgetCallback() {
  $("#widget_accordion").accordion({fillSpace: true});
}

example('http://example.com/', "#divid", widgetCallback);
like image 156
Dave Ward Avatar answered Oct 04 '22 07:10

Dave Ward


you need to invoke the function inside the callback

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback();
});
like image 37
redsquare Avatar answered Oct 04 '22 07:10

redsquare


This should work:

$(targetwidget).load(file, {limit: 25}, callback);
like image 27
peirix Avatar answered Oct 04 '22 05:10

peirix