nathanbweb
nathanbweb

Reputation: 717

pass variable to deferred JSON function?

How can I use myValue in this deferred function so that myQuery is defined?

$.getJSON('lorem.json', function (data) {

    var myKey = $("input").val(); 
    var myValue = data[myKey];

}).then(function () {

    var myQuery = "http://example.com?q=" + myValue ;

    $.getJSON(myQuery, function (info) {
        console.log(info);
...

Upvotes: 2

Views: 946

Answers (1)

Luan Castro
Luan Castro

Reputation: 1184

You can use the parent context

var myValue = null;
$.getJSON('lorem.json', function (data) {

    var myKey = $("input").val(); 
    myValue = data[myKey];

}).then(function () {

    var myQuery = "http://example.com?q=" + myValue ;

    $.getJSON(myQuery, function (info) {
        console.log(info);

Upvotes: 3

Related Questions