18

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.

5 Answers 5

24
$("#auto").autocomplete({
    source: ...
}).autocomplete( "widget" ).addClass( "your_custom_class" );
Sign up to request clarification or add additional context in comments.

3 Comments

Adds a class to the input, but not the associated autocomplete list.
The question refers to the 'drop downs' requiring styling and this only modifies the input.
Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'widget'
19

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.

2 Comments

This is the definitive answer according to the documentation. But it's very important to note that it requires version 1.21.1. It doesn't seem to work on autocomplete in earlier versions.
I think @ron means that version 1.12.1 is required, since that's the latest version. Just to save everyone else a visit to the docs.
16

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);
    };
});

Comments

1

Use this method to add custom classes to drop down box

_renderMenu: function( ul, items ) {
    var that = this;
    $.each( items, function( index, item ) {
        that._renderItemData( ul, item );
    });
    $( ul ).find( "li:odd" ).addClass( "odd" );
}

1 Comment

I added that exact snippet to my code and nothing happened. When does that method supposedly run? I added immediately after the _create: method
-2

I managed to get it working by following the documentation and by updating my jquery version to v1.12.4 at least (required by jquery classes options) and then updating also the jquery-ui version accordingly (v1.12.1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.