Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make matched text bold with jquery ui autocomplete?

I am wondering how to make the matched part of the autocomplete suggestions bold when using jquery ui autocomplete?

So for example if you type in "ja" and the suggestions are javascript and java (like in the example on the jquery ui demo page) then I would like to make "ja" bold in both suggestions.

Anyone knows how to do that?

Thanks a lot for the help...

like image 232
apolka Avatar asked Jul 27 '10 14:07

apolka


People also ask

How do I show bold text in jQuery?

addClass("boldText");

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is UI autocomplete?

Autocomplete mechanism is frequently used in modern websites to provide the users a list of suggestion while typing the beginning word in the text box. It facilitates the user to select an item from the list, which will be displayed in the input field.


2 Answers

I'm not sure why the autocomplete is so bare-bone compared to the other functionalities it contains (e.g. droppable, sortable, draggable etc.).

It should really offer you with a stylable option, e.g. wrapping it with <span class="ui-autocomplete-term">term</span> or something similar.

You could do it like this

It should be pretty self-explanatory; if not, gimme a shout.

like image 190
peol Avatar answered Oct 24 '22 02:10

peol


In jQuery UI 1.11.1, here is the code I used to make it work (case insensitive):

open: function (e, ui) {
    var acData = $(this).data('ui-autocomplete');
    acData
    .menu
    .element
    .find('li')
    .each(function () {
        var me = $(this);
        var keywords = acData.term.split(' ').join('|');
        me.text(me.text().replace(new RegExp("(" + keywords + ")", "gi"), '<b>$1</b>'));
     });
 }
like image 39
Frederic Avatar answered Oct 24 '22 01:10

Frederic