jquery ui autocomplete in a nutshell
HTML
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<input id="account" type="text" style="width: 200px;">
CSS
.selected{
background:red;
}
JavaScript
$(document).ready(function() {
var terms;
var availableTags = [
"Alvin Lim",
"Khia Wijaya",
"Suryo Guritno",
"Adjie Hidayat",
"Yossy Mo",
"Iwan Prasetyo",
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#account" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
/* $('#keyword input#account').css('background','url(../image/arrow-up.png) no-repeat 95% center #bcbcbc'); */
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
open:function(event,ui){console.log("AutoComplete Opened");},
select: function( event, ui ) {
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;
}
}).data('ui-autocomplete')._renderItem = function( ul, item ) {
if($.inArray(item.label, terms) > -1 ){
return $( "<li class='selected'>" + item.label + "</li>" )
.data( "item.autocomplete", item )
.appendTo( ul );
}else{
return $( "<li class='noSelected'>" + item.label + "</li>" )
.data( "item.autocomplete", item...