Thassa Thassa - 4 months ago 9x
jQuery Question

jQuery: Display only matching characters in autocomplete dropdown

I'm making my own dropdown script in jQuery. Things are going well, but I'm running into a problem. Whenever I enter any characters into the text field, all elements in the array I am searching are displayed, instead of the elements that match what's in the textbox.

For example, if I regex search for "ra" against my array of:

var planes = [

I should only see Alara, Fiora, Innistrad and Ravnica in the autocomplete.

Here's a Plunker.

Any help is greatly appreciated.


You need to compare to -1, so it will only show if it is found. You are currently comparing to 1 instead, so it returns true for all items. Probably a typo. Good luck!

            //Grep used to search array but not alter the original.
            var results = $.grep(planes, function(item){
                return RegExp(query, "i")) != *add a - here* 1;
            console.log("Added " + query + " to cache.");
            cache[query] = results;