Liam
Liam

Reputation: 9855

Populate select element using Vanilla JS and JSON file

I'm trying to populate a select element using data from an external json file.

var dropDown = document.getElementById('dropdown');

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // success
    data = JSON.parse(request.responseText);
    console.log(data);

    for (var i = 0, len = data.length; i < len; i++) {
        alert('FOO');
        var data = data[i];
        dropDown.innerHTML(option.name);
    }
};

JSON

{
    "TopLevel": {
        "Second": [
            "data",
            "data",
            "data",
            "data"
        ],
        "Second": [
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            }
        ]
    },
    "TopLevel": {
        "Second": [
            "data",
            "data",
            "data",
            "data"
        ],
        "Second": [
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            }
        ]
    }
}

I've can successfully return my data using the above but for some reason I cant get the loop to run and im unsure what I'm doing wrong.

There's no console errors and my alert doesn't fire...

Upvotes: 1

Views: 2725

Answers (2)

Abhishek Pandey
Abhishek Pandey

Reputation: 13568

You need to write code in this way.

The data is returning object {} therefore the length of the data is undefined and loop is not running.

You need to select particular data which will return array.

Ex: data.TopLevel.Second

var data = {
  "TopLevel": {
    "Second": [
      "data",
      "data",
      "data",
      "data"
    ],
    "Second": [{
        "ThirdLabel": "data",
        "ThirdID": "data"
      },
      {
        "ThirdLabel": "data",
        "ThirdID": "data"
      },
      {
        "ThirdLabel": "data",
        "ThirdID": "data"
      }
    ]
  },
  "TopLevel": {
    "Second": [
      "data",
      "data",
      "data",
      "data"
    ],
    "Second": [{
        "ThirdLabel": "data",
        "ThirdID": "data"
      },
      {
        "ThirdLabel": "data",
        "ThirdID": "data"
      },
      {
        "ThirdLabel": "data",
        "ThirdID": "data"
      }
    ]
  }
}

var dropDown = document.getElementById('dropdown');
debugger;
var options = "";
for (var i = 0, len = data.TopLevel.Second.length; i < len; i++) {
  var item = data.TopLevel.Second[i];
  options += "<option> " + item.ThirdID + "</option>"
}
document.getElementById("dropdown").innerHTML = options;
<select name="" id="dropdown"></select>

Upvotes: 0

robinvrd
robinvrd

Reputation: 1848

The best way to add option to a select is using document.createElement("option") according to W3schools Select add() Method.

var json = { "data": ["orange", "banana", "apple", "pear"] };

var dropdown = document.getElementById("dropdown");

for (var i = 0; i < json.data.length; i++) {
  var option = document.createElement("option");
  option.text = json.data[i];
  option.value = json.data[i];
  dropdown.add(option);
}
<select id="dropdown"></select>

Upvotes: 2

Related Questions