tags:

views:

15

answers:

1

From the jQuery UI documentation:

widget

.autocomplete( "widget" )
Returns the .ui-autocomplete element.

How can I use this in conjunction with actually specifying parameters for the autocomplete? I have a scenario such as this:

$("#employees").autocomplete({
    source: '/roster/home/search'
});

I would like to add a specific CSS class to the ui-autocomplete. I know I can specify a new .ui-autocomplete css class, but that isn't what I need to do. I need to add an existing css class to it. The 'widget' thing seemed to be the appropriate approach, but I'm confused as to how it would be used. Any suggestions? The documentation doesn't make this very clear at all.

+1  A: 

.autocomplete("widget") returns a jQuery object that's a wrapper for the actual
class="ui-autocomplete" element it creates, so you can just call .addClass() on it, like this:

$("#employees").autocomplete("widget").addClass("myClass");

You can test it out here, in your case you can chain it as well:

    $("#employees").autocomplete({
        source: '/roster/home/search'
    }).autocomplete("widget").addClass("myClass");
Nick Craver
See, I'm having the problem of it only adding the right classes AFTER it's run once. So you type in something, the autocomplete runs and displays results, but then the class additions don't take effect until the next time the text box changes.
Stacey
@Stacey - When are you running the code? It you're running it once right after creating it (also hopefully only once) it'll work like in my demo above.
Nick Craver
Nevermind, it was my own bad CSS doing that. Thanks, this helped a lot!
Stacey
@Stacey - welcome :)
Nick Craver