I was using JQuery autocomplete from Jörn Zaefferer to finish a small project for Computer Telephony – IPX PBX user interface. I think this is an awesome plugin ad it is suited very well for my project.

For the project, I need the autocomplete to be able to search all data containing words which begins with the text being typed in the input textbox. However, after browsing through the documentation and try the possible combination of matchContains and matchSubset options of the plugin, it seems that it was not able to do what I want it to do. So, I decided to debug the javascript and go into the code to modify the search algorithm.

When I look into the code, I found this part of code:

function matchSubset(s, sub) {
		if (!options.matchCase)
			s = s.toLowerCase();
		var i = s.indexOf(sub);
		if (options.matchContains == "word"){
			i = s.toLowerCase().search("\\b" + sub.toLowerCase());
		}
		if (i == -1) return false;
		return i == 0 || options.matchContains;
	};

In the fifth line of the function, there is a value check for matchContains option which implies that it received “word” value besides boolean true or false. The code uses the javascript search function with regex using \b tag which means word boundary. This means that it already have the functionality to search for subset contained in each word of the search data.
I immediately changed my code to try to use the option by adding this line:

matchContains:"word"

So, the whole code to activate the autocomplete textbox is as below:

 $("#searchInput").autocomplete(json.Data, {
                    cacheLength:1,
                    delay:0,
                    formatItem: function(row)
                    {
                        var sShow = row.Name + " - (" + row.TypePhone + ") " + row.Phone;
                        return sShow;
                    },
                    matchContains:"word",
                    matchSubset:true,
                    max:100,
                    minChars:1,
                    width:285,
                    selectFirst:true,
                    scrollHeight:"100px"
                });

After that, I try to find in the author’s website and JQuery documentation where in the world that option is being documented. At last I found it in the change log for the latest version 1.1 (per this post date). It should be also written in the JQuery plugin documentation, since it is a very useful feature, at least for me at the time :) . I hope the author would update the documentation accordingly soon.