It is really possible. You can click on widget auto- completion events ( searchand select) to do this:
var triggered = false;
var trigger = "TDI:";
$("input").autocomplete({
source: [...],
search: function() {
if (!triggered) {
return false;
}
},
select: function(event, ui) {
var text = this.value;
var pos = text.lastIndexOf(trigger);
this.value = text.substring(0, pos + trigger.length) +
ui.item.value;
triggered = false;
return false;
},
focus: function() { return false; },
minLength: 0
}).bind("keyup", function() {
var text = this.value;
var len = text.length;
var last;
var query;
var index;
if (triggered) {
index = text.lastIndexOf(trigger);
query = text.substring(index + trigger.length);
$(this).autocomplete("search", query);
}
else if (len >= trigger.length) {
last = text.substring(len - trigger.length);
triggered = (last === trigger);
}
});
Demo here: http://jsfiddle.net/andrewwhitaker/kCkga/
Notes:
- This is a very limited demo. It will not work if you try to autocomplete in the middle of the line.
- To complete this example, you will need to work to find out the position of the cursor in the input field and insert text instead
- There are probably other errors, but I definitely think this is doable. Hope this helps you get started.
source
share