I am trying to access a variable that I'm setting within a function. I need to access it outside of the function. I tried to set the variable first outside the function and then alter it from within the function, but that doesn't seem to work. Here's my code:
location_var =""; //i've tried both location_var and var location_var here
$.getJSON('/v/js/round.js', function(data) {
var location_var = "location.pathname.indexOf('/product-p/test-product.htm')"
for(i=0; i<data.records.length-1; i++){
var location_var = location_var + " || location.pathname.indexOf('" + data.records[i].productcode + "')";
}
});
console.log(location_var);
I just get an empty string when it's logged out. When I log it out from within the JSON function, I get the correct value. I'm basically just looping through a JSON file and printing out the values.
I also tried what the answer said here (How to store a global value (not necessarily a global variable) in jQuery?) but trying to access the variables as $.getJSON.location_var but that didn't work either.
Thanks for your help!