Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Custom Class to jQuery UI's ui-autocomplete Combobox Div

How do I add a custom class to the ui-autocomplete div? I have multiple autocomplete widgets loading on my page and some of their drop downs need to be styled differently so I can't just edit the ui-autocomplete class. I am working with the jQuery UI combobox code (http://jqueryui.com/autocomplete/#combobox) and, by altering that code, I would like to add a class to the created ui-autocomplete div.

like image 914
Colin Avatar asked Oct 18 '13 18:10

Colin


3 Answers

$("#auto").autocomplete({
    source: ...
}).autocomplete( "widget" ).addClass( "your_custom_class" );
like image 75
Ramesh Avatar answered Oct 13 '22 21:10

Ramesh


Simply use the classes argument:

$("#auto").autocomplete({
    classes: {
        "ui-autocomplete": "your-custom-class",
    },
    ...
});

This means that wherever jQuery UI applies the ui-autocomplete class it should also apply your-custom-class.

This is relevant to any jQuery UI widget, not just Autocomplete. See the documentation.

like image 25
Jonathan Potter Avatar answered Oct 13 '22 19:10

Jonathan Potter


Sorry for delay). Have a look at code below.

$(function () {
    $("#auto").autocomplete({
        source: ['aa', 'bb', 'cc', 'dd']
    }).data("ui-autocomplete")._renderItem = function (ul, item) {

        ul.addClass('customClass'); //Ul custom class here

        return $("<li></li>")
        .addClass(item.customClass) //item based custom class to li here
        .append("<a href='#'>" + item.label + "</a>")
        .data("ui-autocomplete-item", item)
        .appendTo(ul);
    };
});
like image 23
Alex Avatar answered Oct 13 '22 21:10

Alex