Jesse C Jesse C - 2 years ago 60
Javascript Question

Load in JSON data into html for loop

Got a JSON array of questions and answers that I want to add into HTML, I can load in the data fine, problem is showing the data into each respective section. Just can't quite get it right as it's only showing the last question and the last option in the array for each section.

JSON data is:

"question": "What kind of event are you looking for",
"option": [
"Incentive Trip",
"Birthday Party",
"Insipiring Holiday",
"Memorable Experience",
"Exciting Concert",
"Crazy Festival",
"Product Launch",
"Stag Do",
"Hen Do",
"Surprise for loved ones"
"question": "What are you looking to achieve?",
"option": [
"Bond up with your Clients",
"Reward the best performers",
"Say thank you to your team"

jQuery code is:

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

// Add the question
$.each( $('section'), function() {

$.each( $('section .balloons'), function() {
for(var i = 0; i < $(element.option).length; i++) {
$('.balloons').html('<p>'+ element.option[i] +'</p>');



HTML is:

<section class="play" id="eventType">
<div class="balloons"></div>
<!-- Achieve -->
<section class="achieve" id="achieve">
<div class="balloons">
<div class="balloon"></div>

Answer Source

You can Try this

$.getJSON('questions.json', function(data) 
    $.each($("section"), function (index){
      var result = data[index];
      var $ballons = $(this).find(".balloons");

      $.each(result.option, function(idx) {
          $ballons.append('<p>'+ result.option[idx] +'</p>');


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download