I am using the jquery autocomplete plugin, and am trying to trigger the autocomplete manually based on certain keys the user enters. I'm trying to bind the autocomplete to the input on the fly so the autocomplete can start looking for matches once a user enters a certain key to trigger "I'm now entering data to be searched on", which could be in the middle of the field, not necessarily at the beginning.
I'm binding a keypress event to an input. When the user enters a certain key, like "=", I want to display all of the elements in the array. When the user then enters a letter it would autocomplete the options that match from the array for that character on. This is more or less trying to mimic what happens in Excel, when a user hits the equals key and sees the functions available, except the "=" key does not have to be the first element in the input, it should autocomplete every time "=" is pressed and unbind that ac every time an option is selected.
var array1 = ['one','two','three'];
$.input1.bind('keypress', function(event) {
var keyCode;
if (event.keyCode > 0) {
keyCode = event.keyCode;
} else if (typeof(event.charCode) != "undefined") {
keyCode = event.charCode;
}
if (String.fromCharCode(keyCode) == '=') {
$.input1.unbind('keypress');
$.input1.autocomplete(array1);
$.input1.blur();
$.input1.focus();
e = $.Event('keydown');
e.which = keyCode;
$.input1.trigger(e);
}
});
Even if I get the autocomplete to trigger, if the user has entered anything before the text that might match, it wouldn't match because of the previous text. So, if the user enters "abd =", the autocomplete is getting "abb =" as its q parameter and not just "=".
Any help is really appreciated, im so stuck!!