Skip to content

Instantly share code, notes, and snippets.

@tbprojects
Created September 21, 2012 08:07
Show Gist options
  • Save tbprojects/3760302 to your computer and use it in GitHub Desktop.
Save tbprojects/3760302 to your computer and use it in GitHub Desktop.
[jQuery] autocompleter matching requirement
var config = {
labelField: '#label_input'
valueField: '#value_input',
source: '/path_to_autocompleter_backend', //responds with json [{label: '...', value: '...'}, {label: '...', value: '...'}]
focus: function(event, ui) {
var self = config;
$(self.field).val( ui.item.label );
return false;
},
change: function(event, ui){
var self = config;
if ($(self.valueField).val() == '') {
$(self.labelField).val('')
}
},
select: function(event, ui){
var self = config;
$(self.valueField).val( ui.item.value );
$(self.labelField).val( ui.item.label );
return false;
},
create: function(event, ui) {
var self = config;
$(self.labelField).keydown(function(e){
var notPermittedKeyCodes = [8,32,46];
var keyCode = parseInt(e.keyCode);
if ($.inArray(keyCode, notPermittedKeyCodes) != -1 || keyCode > 46)
$(self.valueField).val('');
});
};
$(config.labelField).autocomplete(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment