I've created a jQuery autocomplete with a set of predefined values.
$(function() {
var foo = [
"bar",
"baz"
];
$( "#foo-teszt" ).autocomplete({ source: foo });
});
What I would like to do, is that if the user changes the country, the names are changed in the foo. Initially it works, but when I use an AJAX call to change the variables. However, even after that, the variable remains the same. (I guess because the function does not look at the variable each time, but loads only once and reuses the foo the state it was the first time it was called.)
So I tried to invoke both the foo and the foo-teszt autocomplete in hope it will reload the contents of foo. I tried it with
$( function() {
var foo = [
"bar",
"baz"
];
$( "#foo-teszt" ).autocomplete({ source: foo });
})();
And giving the function a name and calling it with the country selects onChange, but neither of them worked, I still got the initial values.
I'm looking for this:
User selects a country from a select.
It's onchange makes an AJAX call, which returns the list of available cities in that country.
And the input field gets to load a new autocomplete with the returned AJAX values. (This is the part not working. The HTML source code script changes to the correct form, but the autocomplete is not loaded with the new variables.)
Dynamically changing javascript variable
I also tried this, while it was still running initially, after the AJAX call I still not call it with the onchange, nor within the AJAX itself.
AJAX is a jQuery like this.
$.ajax({ //jadajada
url: "url",
type: "post", //send it through get method
data: {
method: "baz",
foo: bar
},
success: function(response) {
document.getElementById("bal").innerHTML = response;
},
error: function(xhr) {
//Do Something to handle error
}
});