In jQuery UI v1.8rc3, the autocomplete widget accepts source , which can be either a string, an array, or a callback function. If it is a string, autocomplete does a GET at that url to get options / suggestions. If an array, autocomplete searches, as you indicated, for typed characters at any position in terms of the array. The final option is what you want - a callback function.
From the autocomplete documentation:
The third option, callback, provides maximum flexibility and can be used to connect any data source to autocomplete. The callback receives two arguments:
β’ The request object with a single property called "term" that refers to the value that is currently present in the text input. For example, when a user entered βnew yearsβ in a city field that is configured to perform autocomplete, request.term will hold the βnew yearβ.
β’ The response function, a callback that expects one argument to contain the data offered to the user. This data should be filtered based on the provided term and should be an array in the format allowed for simple local data: String-Array or Object-Array with label / value / both properties.
Code example:
var wordlist= [ "about", "above", "across", "after", "against", "along", "among", "around", "at", "before", "behind", "below", "beneath", "beside", "between", "beyond", "but", "by", "despite", "down", "during", "except", "for", "from", "in", "inside", "into", "like", "near", "of", "off", "on", "onto", "out", "outside", "over", "past", "since", "through", "throughout", "till", "to", "toward", "under", "underneath", "until", "up", "upon", "with", "within", "without"] ; $("#input1").autocomplete({ // The source option can be an array of terms. In this case, if // the typed characters appear in any position in a term, then the // term is included in the autocomplete list. // The source option can also be a function that performs the search, // and calls a response function with the matched entries. source: function(req, responseFn) { var re = $.ui.autocomplete.escapeRegex(req.term); var matcher = new RegExp( "^" + re, "i" ); var a = $.grep( wordlist, function(item,index){ return matcher.test(item); }); responseFn( a ); } });
A few key points:
- call
$.ui.autocomplete.escapeRegex(req.term); , which speeds up the search so that any regular expressions contained in the text entered by the user are treated as plain text. For example, a period (.) Makes sense for a regular expression. I found out about this escapeRegex function by reading the autocomplete source code. - line with
new Regexp() . It specifies a regular expression starting with ^ (Circumflex), which implies that it will only match when typed characters appear at the beginning of the member in the array that you wanted. It also uses the "i" option, which implies case insensitivity. - the
$.grep() utility simply calls the provided function for each term in the provided array. The function in this case simply uses regexp to see if the member in the array matches what was printed. - finally, responseFn () is called with the search result.
working demo: http://jsbin.com/ezifi
what it looks like:

Just a note: I believe that the autocomplete documentation will be pretty immature at the moment. I did not find examples that did this, and I could not find a working document that needed .css files or which .css classes would be used. I learned all this from code verification.
See also how can I configure Autocomplete plug-in settings?