Skip to content

Instantly share code, notes, and snippets.

@jgdoncel
Created November 4, 2013 18:03
Show Gist options
  • Select an option

  • Save jgdoncel/7306719 to your computer and use it in GitHub Desktop.

Select an option

Save jgdoncel/7306719 to your computer and use it in GitHub Desktop.
$("#<%= txtMultipleName.ClientID %>").autocomplete({
source: function (request, response) {
$.getJSON("AutoComplete.ashx", {
term: extractLast(request.term)
}, response);
},
search: function () {
// custom minLength
var term = extractLast(this.value);
if (term.length < 1) {
return false;
}
},
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment