1

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...

4
  • 2
    Please give your JSON data or example how it looks Commented Feb 1, 2019 at 11:19
  • Also bare in mind that this check (request.status >= 200 && request.status < 400) is unsafe because you might no capture codes like 301, 204, etc.. Commented Feb 1, 2019 at 11:23
  • 1
    innerHTML is not a function, and this JSON represents an object Commented Feb 1, 2019 at 11:26
  • If there are no console errors, and your alert doesn't fire, either data.length is 0 or your if statement never returns true. Note that what you've posted as JSON is actually an object instead. Commented Feb 1, 2019 at 11:43

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Robinvrd, how would this work with an external file like my example though?
The same way you did it on your example, if your console.log(data) is working, you have all you need.
0

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>

1 Comment

glad, could help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.