0

I'm using jQuery's autocomplete to display suggestions to the user for a text input field and I'm having a hard time making the matching text in the suggestion list bold. For example, if a user starts to type in "balt", the text "balt" in each of the autocomplete suggestions displayed should be bolded like this:

"Baltimore, USA - Baltimore Intl. (BWI)"

(similar to what Kayak.com does on their 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 is each suggestion that will appear in the suggestions list, i need to 
        // make the substring in ph that matches the searchTerm appear bold; I've tried
        // replacing the substring with the same substring with <strong> tags around it
        // and the .bold() function, neither worked.

        ret.push(ph);
        }

        });

        response(ret);
    }
});

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

ray9209
  • 388
  • 3
  • 14

2 Answers2

1

To achieve this you should use the _renderItem function, which basically renders the items in the result list. In this function you would replace the searched string with a new string surrounded by 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 is each suggestion that will appear in the suggestions list, i need to 
    // make the substring in ph that matches the searchTerm appear bold; I've tried
    // replacing the substring with the same substring with <strong> tags around it
    // and the .bold() function, neither worked.

    ret.push(ph);
    }

    });

    response(ret);
}
}).data("ui-autocomplete")._renderItem = function( ul, item ) {
    return $( "<li>" )
    .attr( "data-value", item.value )
    .append( $( "<a>" ).html( item.label.replace(new RegExp(this.term, 'gi'),"<b>$&</b>") ) )
    .appendTo( ul );
};
0

The problem you're having is you're initializing your autocomplete with a static source and expecting to show different data each time you search.

Instead, put your highlighting process in the response() event to capture the returned data after each search before it renders, like so:

,response: function( event, ui ) {
   //render through the ui object here to update the highlighting on the results.
   if(ui.length>0){
     ui[0].label = "highlighted text";
   }
}
Control Freak
  • 12,965
  • 30
  • 94
  • 145