Vijai Krishna Vijai Krishna - 2 months ago 16x
Javascript Question

auto complete exact match from start using jquery autocomplete from simple array

How do I enable exact match from start of string using jquery autocomplete with input from simple array?

If i have the following in an array:

  • smart

  • oversmart

  • smartland

  • undersmart

  • verysmart

And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.


You just need to modify source parameter as a function to suit your needs. Like this:

Update: Adding code to answer:

var acList = ['smart', 'oversmart', 'smartland', 'undersmart', 'verysmart'];
    source: function (request, response) {
        var matches = $.map(acList, function (acItem) {
            if (acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0) {
                return acItem;