Skip to content

Instantly share code, notes, and snippets.

@sound-code
Created April 30, 2015 14:17
Show Gist options
  • Save sound-code/33b971680f973784e304 to your computer and use it in GitHub Desktop.
Save sound-code/33b971680f973784e304 to your computer and use it in GitHub Desktop.
autocompleter-js
(function ($) {
'use strict';
$.fn.autocompleter = function (options) {
var settings = {
url_list: '',
url_get: '',
min_length: 1
};
return this.each(function () {
if (options) {
$.extend(settings, options);
}
var $this = $(this), $fakeInput = $('<input type="text" name="fake' + $this.attr('name') + '">');
$this.hide().after($fakeInput);
$fakeInput.autocomplete({
source: settings.url_list,
minLength: settings.min_length,
select: function (event, ui) {
$this.val(ui.item.id);
}
});
if ($this.val() !== '') {
$.ajax({
url: settings.url_get + $this.val(),
success: function (name) {
$fakeInput.val(name);
}
});
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment