Skip to content Skip to sidebar Skip to footer

Make Matched Letters Highlighted In Autocomplete In Javascript/jquery

I have a set of airport names along with their codes to be displayed in autocomplete. E.g. Amsterdem (AMS) I want to match the value entered in the search box,I want to display the

Solution 1:

you have to change

var re = newRegExp("^" + this.term, "i") ;

with this code

var re = newRegExp(this.term, "ig") ;

this will replace all instance in your string

"^" denotes start of string, and "g" in regular expression works on all instance

here is working example

replace your code with this one

function () {
var oldFn = $.ui.autocomplete.prototype._renderItem;
$.ui.autocomplete.prototype._renderItem = function( ul, item) {
    var re = newRegExp(this.term, "ig") ;
    var t = item.label.replace(re,"<span style='font-weight:bold;'>" + this.term + "</span>");
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>" + t + "</a>" )
        .appendTo( ul );
};
};

Post a Comment for "Make Matched Letters Highlighted In Autocomplete In Javascript/jquery"