Bart Bart - 1 year ago 261
Javascript Question

jQuery UI Autocomplete use startsWith

I'm using the jQuery UI Autocomplete with a local datasource (

source: myArray
). I want the autocomplete to propose only the results that start with the string entered instead of the default case-insensitive contains search. Is there a simple solution for this or do I have to provide my custom search/source callback?

Answer Source

Currently I've done it this way, not sure if there is a better solution:

source: function(request, response) {
    var filteredArray = $.map(orignalArray, function(item) {
        if( item.value.startsWith(request.term)){
            return item;
            return null;

This approach also made it possible to impose a limit (e.g. 10 items) on the amount of items to be shown.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download