javascript - Array created from .get() won't populate html select -
this question has answer here:
- how return response asynchronous call? 21 answers
here's rundown. have external .txt file has number of locations (one per line) need have loaded script populate dropdown. i'm using .get() pull in data, , putting array. looks correct, dropdown isn't populated. doing tests, found if manually populate array when declare data declare there loads fine. also, if try doing array such turning string nothing happens, , if try checking length reports 0. i've tried making new array , pushing content of first it, no dice. i've tried using makearray, didn't work.
here's code far:
var siteidarray = []; var path = "*snipped*/siteid.text"; var option = ""; $.get(path, function(data) { siteidarray = data.split("\n"); console.log(siteidarray); }); (i = 0; < siteidarray.length; i++) { option += '<option value="'+ siteidarray[i] + '">' + siteidarray[i] + '</option>'; } $('#siteid').append(option);
here's test of siteidaarray contains (full array has total of 62 items):
["an", "az", "ba", "ca"]
and siteid.txt looks this:
an az ba ca
$.get
async call, have work response object inside callback or pass function:
$.get(path, function(data) { siteidarray = data.split("\n"); console.log(siteidarray); (i = 0; < siteidarray.length; i++) { option += '<option value="'+ siteidarray[i] + '">' + siteidarray[i] + '</option>'; } $('#siteid').append(option); });
Comments
Post a Comment