Ramnath
Ramnath

Reputation: 55715

Replace Inline JSON with a getJSON Call

I am new to jQuery and javascript and this is probably a beginner's question.

I am using a javascript quiz library SlickQuiz to make quizzes. The quiz uses the following callback to populate the page

$(function () {
    $('#slickQuiz').slickQuiz({json: {YOUR_JSON_HERE}});
});

I want to place the quiz json in a separate file and use $.getJSON to pass it. I tried the following, but it did not work.

$(function(){
  $.getJSON('path/to/json', function(quizJSON){
     $('#slickQuiz').slickQuiz()
  });
});

I have posted it as an issue on github, but thought it might be a trivial jQuery novice issue and am posting it here.

Upvotes: 1

Views: 804

Answers (2)

Billy Moon
Billy Moon

Reputation: 58601

$(function(){
  $.getJSON('path/to/json', function(quizJSON){
     // you need to pass the json to your quiz function
     $('#slickQuiz').slickQuiz({json: quizJSON});
  });
});

Upvotes: 1

Waleed Khan
Waleed Khan

Reputation: 11467

Pass the JSON as a parameter to the quiz function:

$(function(){
  $.getJSON('path/to/json', function(quizJSON){
     $('#slickQuiz').slickQuiz({
         json: quizJSON // <--
     });
  });
});

Upvotes: 2

Related Questions