0

I'm trying to figure out why when I call setupControlBox from within the $.getJSON call it works (in the if condition) but if I call it from the else condition it throws an undefined error (I set opts.load_url to false). Does the call have to be encapsulated in $.someFunction()?

// Load existing form data

if (opts.load_url) {
    $.getJSON(opts.load_url, function(json) {
        form_db_id = json.form_id;
        fromJson(json.form_structure);
        opts.loaded(json);
        var controlBox = setupControlBox(opts.control_box_target);
    });
} else {
    var controlBox = setupControlBox(opts.control_box_target);      
}
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
  • Welcome to the wonderful world of **async**! You can't do that. – SLaks Jun 08 '14 at 16:30
  • 1
    I wouldn't worry about `setupControlBox`, but rather where `controlBox` is accessible from and which value it has. You might want to have a look at [Why is my variable undefined after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/218196) – Felix Kling Jun 08 '14 at 16:31

0 Answers0