The CreatorCon Call for Content is officially open! Get started here.

How do i pass value to a callback function

Ravish Shetty
Tera Guru

Hello all,

Is there a way to pass value to the callback function when we use GlideAjax.

function onChange (){

var temp = '123';

var x = new GlideAjax ('ABC');

x.addParam ('sysparm_name','123');

x.getXML(callBack,temp);

}

function callBack (response,temp){

alert(temp);

}

Here i want to pass the value of variable temp to the callback function without declaring it as a global variable.

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hello Ravio,



You can do it   with a closure:



function onChange(){


        var temp = '123';


        var x = new GlideAjax('ABC');


        x.addParam ('sysparm_name', '123');


        x.getXML( function (response) { callback (response, temp); });


}



function callBack (response, temp){


        alert(temp);


}


View solution in original post

8 REPLIES 8

randrews
Tera Guru

not really as familiar with ajax as I should be but have y ou tried this...



in the function put in return temp



now when you call it.. call it like this...



alert(function callBack (response,temp));


Hello Doug,



If possible, can you show it in the form of code on exactly how this will work?


edwin_munoz
Mega Guru

Hello Ravio,



You can do it   with a closure:



function onChange(){


        var temp = '123';


        var x = new GlideAjax('ABC');


        x.addParam ('sysparm_name', '123');


        x.getXML( function (response) { callback (response, temp); });


}



function callBack (response, temp){


        alert(temp);


}


Thanks Edwin. This worked!