Bernie Bernie - 1 year ago 224
jQuery Question

How to use source: function()... and AJAX in JQuery UI autocomplete

I need a little bit help with JQuery UI Autocomplete. I want my textfield (

) display names from an AJAX request. This is what I have:

source : function(request, response) {
var name = jQuery("input.suggest-user").val();
jQuery.get("usernames.action?query=" + name, function(data) {
console.log(data); // Ok, I get the data. Data looks like that:
test = data; // ["", "",""]
return test; // But what now? How do I display my data?
minLength : 3

Any help is very much appreciated.

Answer Source

Inside your AJAX callback you need to call the response function; passing the array that contains items to display.

    source: function (request, response) {
        jQuery.get("usernames.action", {
            query: request.term
        }, function (data) {
            // assuming data is a JavaScript array such as
            // ["", "",""]
            // and not a string
    minLength: 3

If the response JSON does not match the formats accepted by jQuery UI autocomplete then you must transform the result inside the AJAX callback before passing it to the response callback. See this question and the accepted answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download