jQueryUI 1.8.14 Autocomplete
HTML
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.14/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.14/themes/black-tie/jquery-ui.css">
<input id="tagSearch" />
CSS
span.count {
font-style: italic;
color: #C0C0C0;
}
.hidden { display: none; }
JavaScript
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
jQuery('#tagSearch').bind('keydown', function(event) {
if (event.keyCode === jQuery.ui.keyCode.TAB && jQuery(this).data('autocomplete').menu.active) {
event.preventDefault();
}
}).autocomplete({
autoFocus: true,
source: function(request, add) {
$.ajax({
url: "http://api.stackoverflow.com/1.1/tags",
data: {
filter: extractLast(request.term),
pagesize: 5
},
type: "POST",
success: function(response) {
add($.map(response.tags, function(item) {
return item.name;
}));
},
jsonp: "jsonp",
dataType: "jsonp"
});
},
search: function() {
console.log('search');
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
return false;
},
select: function(event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push('');
this.value = terms.join(', ');
return false;
},
minLength: 3
});