I have this code :
<script type="text/javascript">
var myList = [ "Avellino", "Enna", "Frosinone" ];
$("#myTextBox").autocomplete({
source: myList,
appendTo: "#myOwnMenuProvince"
});
</script>
Now, if I digit e I’d like to see only words in the list that start with e, not that contain e. So, in the example above, only Enna.
How can I do it?
By default, the plugin uses the method
$.ui.autocomplete.filter. This method does a grep using a regular expression on the label/value properties of the data and returns the filtered data. Let’s inspire ourselves from that to make our filtering.To use your own filtering method, use the
sourceoption as a callback.the
requestparameter is an object containing one propertyterm. It contains the text to search for, basically the value of the input field. So if you type “en” in the input field,request.termwill have that value.the
responseparameter is a callback. You call it, passing the filtered data, to display the data, basically show the results popup menu.So basically, you would filter your data and pass the filtered array to the `response´ callback.
Here’s one way to do it:
DEMO