-2

I have a python file which return a json file.It contains only a list like this : [1,2,3,4,5,6,7,] How to make this get populated in a select box in HTML page.

Code :

$.getJSON('data.json', function(data) {
    destinations = data[]

    $.each(destinations, function(id, destination) {
        destination = destination[]
    })
});

what as to be done?

qwww
  • 1,313
  • 4
  • 22
  • 48
  • Not sure what you want, but this can't be right: destination = destination[], that, or your data would be something like[[[1],[2],[3]]]? Then still I'd expect the assignment to include the key for the position in the array, like destination = destination[0]. – Jonathan Sep 21 '18 at 11:33
  • And perhaps you should include the jQuery tag and remove the python one, as it appears to be irrelevant what process it is that provides the json. – Jonathan Sep 21 '18 at 11:37

2 Answers2

0

Previously answered here

$.getJSON('data.json', function(data) {
  var sel = $('<select>').appendTo('body');
  $(data).each(function() {
   sel.append($("<option>").attr('value',this).text(this));
  });
});

In your case not this.val and this.text as your array does not contains objects but only integers.

Jonathan
  • 1,355
  • 14
  • 22
0

Try this:

$.getJSON('data.json', function(data) {
    destinations = data[];

    $("#selectid").clear();
    $.each(destinations, function(id, destination) {
        $("#selectid").append("<option value="+destination[id]+">"+destination[id]+"</option>");
    });
});

Hope this is your expectation

Shreesha
  • 71
  • 2
  • 5