-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathjquery.ui.autocomplete.autoSelect.js
More file actions
41 lines (38 loc) · 1.56 KB
/
jquery.ui.autocomplete.autoSelect.js
File metadata and controls
41 lines (38 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* jQuery UI Autocomplete Auto Select Extension
*
* Copyright 2010, Scott González (http://scottgonzalez.com)
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* http://github.com/scottgonzalez/jquery-ui-extensions
*
* Modified like 25 to have separate matcher calls
* Added line 31 to set the value of the text box like jq does for us
* Modified line 35 to prevent clearing initial value on losing focus
*/
(function( jQuery ) {
jQuery.ui.autocomplete.prototype.options.autoSelect = true;
jQuery( document ).on( "blur", ".ui-autocomplete-input", function( event ) {
var autocomplete = jQuery( this ).data( "ui-autocomplete" );
if ( !autocomplete || !autocomplete.options.autoSelect || autocomplete.selectedItem ) { return; }
var matcher = new RegExp( "^" + jQuery.ui.autocomplete.escapeRegex( jQuery(this).val() ) + "$", "i" );
/*alert("value: " + jQuery(this).val() + " matcher: ");*/
autocomplete.widget().children( ".ui-menu-item" ).each(function() {
var item = jQuery( this ).data( "ui-autocomplete-item" );
/*alert("matched? " + matcher.test(item.value) + " item.value: " + item.value);*/
if ( matcher.test( item.label ) || matcher.test( item.value ) || matcher.test( item ) ) {
autocomplete.selectedItem = item;
return false;
}
});
if ( autocomplete.selectedItem ) {
jQuery( this ).val(autocomplete.selectedItem.value);
/*alert("triggering select");*/
autocomplete._trigger( "select", event, { item: autocomplete.selectedItem } );
}
else if (jQuery(this).val() != autocomplete.options.initialValue) {
/* Nothing valid was selected */
jQuery( this ).val("");
}
});
}( jQuery ));