-1

im trying to pass a value out of a get jQuery function here is the example:

function a(){

   var test="";    

   $.get("js/getDATEinvoice.php?inv=" + invoice, function(html) {               
      test=html;            
   });

alert(test);
}

I have tried with returns and i just can not get the value...

I have to call the function inside a function so it can get the value and proceed doing stuff.

Thanks in advance!

André Alvarez
  • 131
  • 1
  • 4
  • 13
  • The `A` in `AJAX` stands for *asynchronous*. You can't get the value immediately after you start the ajax call because it isn't done yet. You need to access it inside a callback. – Matt Burland Apr 02 '14 at 15:47

2 Answers2

1

Do this:

$.get("js/getDATEinvoice.php?inv=" + invoice, function(html) {               
      test=html;  
      alert(test);          
   });

A in Ajax means asynchronous. Also the function isn't guaranteed to return something from the result when called.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

You just need to declare your variable outside the function scope

 var your_Var = '';
 function yourFunc(){
      your_Var =1;

 };
 alert(your_Var);
Y.Puzyrenko
  • 2,166
  • 1
  • 15
  • 23