Skip to content

Instantly share code, notes, and snippets.

@aindong
Created June 3, 2015 02:13
Show Gist options
  • Save aindong/526f9088afeb67220c5f to your computer and use it in GitHub Desktop.
Save aindong/526f9088afeb67220c5f to your computer and use it in GitHub Desktop.
Jquery UI autocomplete select extension
(function( $ ) {
$.ui.autocomplete.prototype.options.autoSelect = true;
$( ".ui-autocomplete-input" ).live( "blur", function( event ) {
var autocomplete = $( this ).data( "autocomplete" );
if ( !autocomplete.options.autoSelect || autocomplete.selectedItem ) { return; }
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" );
autocomplete.widget().children( ".ui-menu-item" ).each(function() {
var item = $( this ).data( "item.autocomplete" );
if ( matcher.test( item.label || item.value || item ) ) {
autocomplete.selectedItem = item;
return false;
}
});
if ( autocomplete.selectedItem ) {
autocomplete._trigger( "select", event, { item: autocomplete.selectedItem } );
}
});
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment