0

I am a javascript/jquery noob, and am going crazy as to why I cannot get the values I receive in a jquery function to also be available outside of that function. Here's what I have inside a Javascript function:

lines=[];
$(document).ready(function(){
    $.get("sampledata.txt", function(dataInput, status){
        lines = dataInput.split(",");
        alert("Lines inside: " + lines);       //Prints out fine here
    });
});
alert("Lines outside: " + lines);

The alert "Lines outside" returns blank, but "Lines inside" returns my list of ten numbers, comma separated. I tried to declare lines outside of the jquery function, but it seems that it's ignoring the changes made inside the jquery function. Everything works as I would like inside the jquery function.

sampledata.txt looks like this:

10,20,30,40,50,60,70,80,90,100
jrcichra
  • 335
  • 5
  • 16
  • 5
    Possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) (see also http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron for an exact duplicate) – JJJ Oct 03 '15 at 17:22
  • @Juhana Sorry for the duplicate, I wasn't sure what I needed to look for to get this answered, so thanks for leading me in the right direction! – jrcichra Oct 03 '15 at 17:32

0 Answers0