Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI autocomplete refresh data

I use jQuery ui autocomplete feature.

var colors;

$(document).ready(function(){
        loadColors();
        $('#empf').autocomplete(colors);
}

function loadColors(){
 colors = new Array(getNumColor());
//in a loop save the colors to array using colors[i] = ...
}

function addColor(){
    ...
    color[n] = color;
}

When the user enters new color it is saved to the color array. I switch to the autocomplete form but the entered data is not aviable until I refresh the page.

Any ideas how to make the new color avialable for autocomplete?

like image 914
UpCat Avatar asked Jan 15 '11 11:01

UpCat


1 Answers

When you update the color, you need to also update the source that autocomplete uses, like this:

function addColor() {
    //add colors
    $('#empf').autocomplete("option", { source: colors });
}

Here's a sample demo doing this, adding a color and updating the autocomplete source once a second.

like image 146
Nick Craver Avatar answered Sep 28 '22 19:09

Nick Craver