How to patch *just one* instance of Autocomplete on a page?
Asked Answered
S

3

21

This answer -- jQueryUI: how can I custom-format the Autocomplete plug-in results? -- describes how to monkeypatch the jqueryUI autocomplete widget, so that it displays things in a particular way. The approach it uses is to replace a function on the $.ui.autocomplete.prototype.

This means that all autocomplete widgets will get this patch.

Is there a way to patch the autocomplete widget for just one input element? What is it?

When I examine $('$input').autocomplete , I don't see any of the autocomplete fns there (_renderItem, _renderMenu, _search, etc).

Skeen answered 28/5, 2011 at 17:14 Comment(0)
D
26

Check out the custom data and display demo. This demo is not modifying the prototype object of the autocomplete widget, meaning that only that instance of the widget is effected:

$("selector").autocomplete({ ... }).data( "autocomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
};

Here's a working demo: http://jsfiddle.net/vJSwq/

Dying answered 28/5, 2011 at 22:4 Comment(6)
That doesn't seem to work in IE, the error is always is "data(...) is null or not an object." Is data actually unsupported in this case? Examples from api.jquery.com/data work properly, and the only warning on that page is that IE doesn't allow fiddling with XML in this way.Sarraceniaceous
Try using .data('uiAutocomplete') instead. This was changed in recent versions of jQueryUI.Dying
If you are getting a "Attempted to assign to readonly property." in webkit. Try using .data('ui-autocomplete').Delgado
At this time, the last example on this page suggests using .data('ui-autocomplete').Wintry
@AndrewWhitaker i am facing a problem when i hover the data the data disappear. i am using autocomplete jquery uiArela
@HassaanKhan: It would be best to open a new questionDying
B
6

The code from Andrew Whitaker just work for one autocomplete input. If you select more than one input-element, the second autocomplete widget dosn't show any entrys. You have to add a foreach to handel all selected input elements like mentioned here

$("selector")
.autocomplete({ ... })
.each(function () {
    $(this).data("autocomplete")._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.label + "<br>" + item.desc + "</a>")
            .appendTo(ul);
    };
);
Brainwash answered 23/2, 2012 at 12:46 Comment(1)
I just ran into this today, with multiple instances. Thank you!Delgado
L
1

For recent versions of jQuery(1.8+) / jQuery UI (1.10+), you should use:

$("selector").autocomplete({ ... }).data('uiAutocomplete')._renderItem = function( ul, item ) {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
    .appendTo( ul );
};
Lancelot answered 6/2, 2013 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.