I have an issue with the auto complete in js. Here the following code works well for me:
$(function(){
$( "#txtAuto" ).autocomplete({
source: ["Choice1","Choice2"],
minLength:2
});
});
But when I change this to the code below for testing it doesn't gives me choices:
$(function(){
$( "#txtAuto" ).autocomplete({
source: function( request, response ){
$.ajax({
url: "test.ewd",
success: function(data){
var res=data.match('\\[[^\\]]*]');
return ["Choice1", "Choice2"];
}
});
},
minLength:2
});
});
Any one tell me where I did mistake?
The core of the problem is that the ajax-function is asynchronous. It is not executed at the same time as the rest of your code, but rather when the response for your ajax-request (which probably takes 10-100 ms to complete) is available.
So, you can't return
the values from the success
-function. Instead, you must pass them in to the response
function. like this:
success: function(data) {
var res=data.match('\\[[^\\]]*]');
response(["Choice1", "Choice2"]);
}