Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Passing variables to function upon successful $.get request

Is it possible to pass predefined variables to functions called upon successful jQuery $.get request?

As in the example below the PHP script would return "bar":

var extra = "foo";
$.get(baar.php, function(data, extra){
    alert(extra + data);
});

Thus my goal being an alert box proclaiming "foobar" to the world.

Thanks.

like image 837
Matte Avatar asked May 07 '13 20:05

Matte


1 Answers

You don't need to pass it, extra will be available inside the callback because of how JavaScript scoping works. So:

var extra = "foo";
$.get('baar.php', function(data){
    alert(extra + data);
});
like image 158
bfavaretto Avatar answered Oct 22 '22 04:10

bfavaretto