Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:44:48+00:00 2026-06-11T05:44:48+00:00

I have the following JSON: var questions = { section: { 1: question: {

  • 0

I have the following JSON:

var questions = {
    section: {
        "1": question: {
            "1": {
                "id" : "1a",
                "title": "This is question1a"
            },
            "2": {
                "id" : "1b",
                "title": "This is question2a"
            }
        },
        "2": question: {
            "1": {
                "id" : "2a",
                "title": "This is question1a"
            },
            "2": {
                "id" : "2b",
                "title": "This is question2a"
            }
        }
    }
};

NOTE: JSON changed based on the answers below to support the question better as the original JSON was badly formatted and how it works with the for loop below.

The full JSON will have 8 sections and each section will contain 15 questions.

The idea is that the JS code will read what section to pull out and then one by one pull out the questions from the list. On first load it will pull out the first question and then when the user clicks on of the buttons either option A or B it will then load in the next question until all questions have been pulled and then do a callback.

When the button in the appended list item is clicked it will then add it to the list below called responses with the answer the user gave as a span tag.

This is what I have so far:

    function loadQuestion( $section ) {

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

    for (var i = 0; i < data.length; i++) {

        var item = data[i];

        if (item === $section) {
            $('#questions').append('<li id="' + item.section.questions.question.id + '">' + item.section.questions.question.title + ' <button class="btn" data-response="a">A</button><button class="btn" data-response="b">B</button></li>');
        }
    }
});

}

    function addResponse( $id, $title, $response ) {

        $('#responses').append('<li id="'+$id+'">'+$title+' <span>'+$response+'</span></li>');

    }

    $(document).ready(function() {

        // should load the first question from the passed section
        loadQuestion( $('.section').data('section') );

        // add the response to the list and then load in the next question
        $('button.btn').live('click', function() {

            $id = $(this).parents('li').attr('id');
            $title = $(this).parents('li').html();
            $response = $(this).data('response');

            addResponse( $id, $title, $response );

            loadQuestion ( $('.section').data('section') );

        });

    });

and the HTML for the page (each page is separate HTML page):

<div class="section" data-section="1">

            <ul id="questions"></ul>

            <ul id="responses"></ul>

        </div>

I’ve become stuck and confused by how to get only the first question from a section and then load in each question consecutively for that section until all have been called and then do a callback to show the section has been completed.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:44:50+00:00Added an answer on June 11, 2026 at 5:44 am
    1. Do not have multiple id’s in html called “section.”
    2. Do not have multiple keys in your JSON on the same level called “section”. Keys in JSON on the same level should be unique just as if you are thinking about a key-value hash system. Then you’ll actually be able to find the keys. Duplicate JSON keys on the same level is not valid.

    One solution can be section1, section2, etc. instead of just section. Don’t rely on data-section attribute in your HTML – it’s still not good if you have “section” as the duplicate html id’s and as duplicate JSON keys.

    If you have only one section id in HTML DOM, then in your JSON you must also have just one thing called “section” e.g.:

    var whatever = {
                        "section" : { 
                                   "1":  {
                                            "question" : {
                                                           "1" : {
                                                              "id" : "1a",
                                                              "title" : "question1a"
                                                                  },
                                                           "2" : {
                                                              "id" : "2a",
                                                              "title"  : "question2a"
                                                                  }
                                                         }
                                          },                                 
                                    "2":  {
                                            "question" : {
                                                           "1" : {
                                                              "id" : "1a",
                                                              "title" : "aquestion1a"
                                                                  },
                                                           "2" : {
                                                              "id" : "2a",
                                                              "title"  : "aquestion2a"
                                                                  }
                                                         }
                                          }
                                     }
                   }
    console.log(whatever.section[1].question[1].title); //"question1a"
    

    To get question, do something like this:

       function loadQuestions(mySectionNum) {
    
           $.getJSON('whatever.json', function(data){
    
            var layeriwant = data.section[mySectionNum].question;
    
               $.each(layeriwant, function(question, qMeta) {
                   var desired = '<div id="question-' + 
                                  qMeta.id +
                                  '"' + 
                                  '>' + 
                                  '</div>';
                   $("#section").append(desired);
                   var quest = $("#question-" + qMeta.id);
                   quest.append('<div class="title">' + qMeta.title + '</div>');
                   //and so on for question content, answer choices, etc.
               });
    
    
    
           });
       }
    

    then something like this to actually get the questions:

        function newQuestion(){
           var myHTMLSecNum = $("#section").attr('data-section');
           loadQuestions(myHTMLSecNum);
        }
    
        newQuestion();
    
      //below is an example, to remove and then append new question:
    
        $('#whatevernextbutton').on('click',function(){
           var tmp = parseInt($("#section").attr('data-section'));
           tmp++;
           $("#section").attr('data-section', tmp);
           $("#section").find('*').remove();
           newQuestion();
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

with reference of this question Sort json array I have the following JSON String
I have the following json object. var json1 = {00 : 00, 15 :
Environment used: ASP.NET, jQuery I have the following AJAX call: var tempVar = JSON.stringify({plotID:currentId});
I have the following JSON String. var jsonString = '{J:4,0:M, J:5,0:N}' If I parse
I have the following JSON: var json = { system : { world :
I have the following code: var json = MyObject .Select(p => new { id
ok i have this following code that parses JSON from an ajax response using
I have the following JSON String { name:Product, properties: { id: { type:number, description:Product
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have the following JSON structure: [{ id:10, class: child-of-9 }, { id: 11,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.