jQuery Autocomplete - how to make matching text bold -



jQuery Autocomplete - how to make matching text bold -

i'm using jquery's autocomplete display suggestions user text input field , i'm having hard time making matching text in suggestion list bold. example, if user starts type in "balt", text "balt" in each of autocomplete suggestions displayed should bolded this:

"baltimore, usa - baltimore intl. (bwi)"

(similar kayak.com on search form).

js:

var airportlist = [ {"iata":"bwi","city":"baltimore","airp":"baltimore intl.","country":"usa"}, {"iata":"sea","city":"seattle","airp":"seattle tacoma intl.","country":"usa"}, {"iata":"lax","city":"los angeles","airp":"los angeles intl.","country":"usa"}, {"iata":"jfk","city":"new york","airp":"john f. kennedy intl.","country":"usa"}]; $("#input1").autocomplete({ minlength: 3, source: function(request, response){ var searchterm = request.term.tolowercase(); var ret = []; var ph; $.each(airportlist, function(i, airport){ if (airport.city.tolowercase().indexof(searchterm) === 0 || airport.iata.tolowercase().indexof(searchterm) !== -1 || airport.country.tolowercase().indexof(searchterm) === 0 || airport.airp.tolowercase().indexof(searchterm) === 0) { ph = airport.city + ', ' + airport.country + ' - ' + airport.airp + ' (' + airport.iata + ')'; // ph each suggestion appear in suggestions list, need // create substring in ph matches searchterm appear bold; i've tried // replacing substring same substring <strong> tags around // , .bold() function, neither worked. ret.push(ph); } }); response(ret); } });

fiddle: http://jsfiddle.net/ray9209/v9o71l11/2/

to accomplish should utilize _renderitem function, renders items in result list. in function replace searched string new string surrounded tag.

var airportlist = [ {"iata":"bwi","city":"baltimore","airp":"baltimore intl.","country":"usa"}, {"iata":"sea","city":"seattle","airp":"seattle tacoma intl.","country":"usa"}, {"iata":"lax","city":"los angeles","airp":"los angeles intl.","country":"usa"}, {"iata":"jfk","city":"new york","airp":"john f. kennedy intl.","country":"usa"}]; $("#input1").autocomplete({ minlength: 3, source: function(request, response){ var searchterm = request.term.tolowercase(); var ret = []; var ph; $.each(airportlist, function(i, airport){ if (airport.city.tolowercase().indexof(searchterm) === 0 || airport.iata.tolowercase().indexof(searchterm) !== -1 || airport.country.tolowercase().indexof(searchterm) === 0 || airport.airp.tolowercase().indexof(searchterm) === 0) { ph = airport.city + ', ' + airport.country + ' - ' + airport.airp + ' (' + airport.iata + ')'; // ph each suggestion appear in suggestions list, need // create substring in ph matches searchterm appear bold; i've tried // replacing substring same substring <strong> tags around // , .bold() function, neither worked. ret.push(ph); } }); response(ret); } }).data("ui-autocomplete")._renderitem = function( ul, item ) { homecoming $( "<li>" ) .attr( "data-value", item.value ) .append( $( "<a>" ).html( item.label.replace(new regexp(this.term, 'gi'),"<b>$&</b>") ) ) .appendto( ul ); };

jquery autocomplete jquery-autocomplete

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -