Here's one way: See it in action at jsFiddle.
var availableTags = [
"win the day",
"win the heart of",
"win the heart of someone"
];
var autoCompNodeId = 'tags';
$( "#" + autoCompNodeId ).autocomplete ( {
source: function (requestObj, responseFunc) {
var matchArry = availableTags.slice (); //-- Copy the array
var srchTerms = $.trim (requestObj.term).split (/\s+/);
//--- For each search term, remove non-matches.
$.each (srchTerms, function (J, term) {
var regX = new RegExp (term, "i");
matchArry = $.map (matchArry, function (item) {
return regX.test (item) ? item : null;
} );
} );
//--- Return the match results.
responseFunc (matchArry);
},
open: function (event, ui) {
/*--- This function provides no hooks to the results list, so we have to trust the
selector, for now.
*/
var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
var srchTerm = $.trim ( $("#" + autoCompNodeId).val () ).split (/\s+/).join ('|');
//--- Loop through the results list and highlight the terms.
resultsList.each ( function () {
var jThis = $(this);
var regX = new RegExp ('(' + srchTerm + ')', "ig");
var oldTxt = jThis.text ();
jThis.html (oldTxt.replace (regX, '<span class="srchHilite">$1</span>') );
} );
}
} );
Because the old highlight function is no longer available, we use the open
event to highlight the search terms. Pay special attention not to highlight inside tags.
win t
. I think you'll see a problem. – Impassioned