autocomplete unique
start fro autocomplete with unique values
HTML
<input type="text" class="ac"/>
<input type="text" class="ac"/>
<input type="text" class="ac"/>
JavaScript
var values = ["one","two","three","three"];
var usedValues = [];
var newValue = "";
$(".ac").autocomplete({
source: values,
select:function(event,ui){
// if the selected values exists in values array
// move it to used values array
if($.inArray(ui.value,values)){
var used = values.splice(ui.item.value,1)[0];
if(used) usedValues.push(used);
console.log(usedValues);
console.log(values);
}
},
search: function(event,ui){
console.log("i will be triggered before a search");
console.log(arguments);
},
change:function(event,ui){
// add our new value to used values array
if($(event.currentTarget).val() == newValue){
// if value already exists, return
if($.inArray(newValue,usedValues)>-1) return;
// else add the value to used values array
usedValues.push(newValue);
var uiNew = ui;
uiNew.item = {
label:newValue,
value:newValue
};
}
},
response: function(event,ui){
// if the value doesn't exist in the source property
// create this new value based on the value of input
if(ui.content.length == 0 && $(this).val() !==""){
newValue = $(this).val();
}
}
});