$.get
is an asynchronous function, so by the time autocomplete
method is executed, countries
would have got no element from the file. So, move the autocomplete
function to the callback of $.get
like this
$.get('test.txt', function(data) {
var array = data.split(',');
$('#autocomplete').autocomplete({
lookup: array,
minChars: 2,
onSelect: function(suggestion) {
//I will put something here soon.
}
});
});
Also, you don't have to populate countries
like you did in the question. You can simply pass the array returned by JavaScript's split
function, as it is.