2

I am trying to implement jQuery autocomplete with a custom drop down menu. I am able to customize menu items with the data()._renderItem method (commented out) ,but this disables the menu "Select" functionality. If I attempt to customize menu items via the "label" field The "Select" functionality works but my menu item HTML is interpreted as a string. Can anyone suggest a clean way of accomplishing this.

$("input#selectedInput")

.bind("autocompleteselect", function (event, ui) {
alert("Sel item " + JSON.stringify(ui.item.json));
})



.autocomplete({
appendTo: "#list",
source: function (request, response) {
    //alert("success");
    $.ajax({
        //url: "http://itunes.apple.com/search?term=jack+johnson&entity=musicTrack",
        url: "Example REST URL",
        dataType: "jsonp",
        data: {
            featureClass: "P",
            style: "full",
            maxRows: 12,
            name_startsWith: request.term
        },

        success: function (data) {
            response($.map(data.results, function (item) {
                itunesJson = item;
                return {
                    label: "<li><img src='" + item.artworkUrl30 + "' alt='no photo'/>" + item.trackName + "</li>",
                }
            }));
        },
    });
}
})
/*
        .data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append("<img src='"+item.value+"' alt='no photo'/>"+ item.label)
            .appendTo( ul );
    };
        */
Charles
  • 50,943
  • 13
  • 104
  • 142
Ben Pearce
  • 6,884
  • 18
  • 70
  • 127

3 Answers3

0

Try to read here: http://www.devbridge.com/projects/autocomplete/jquery/. This is what I use when I need autocomplete. It's really easy to configure. And please tell me, if it doesn't fit your needs.

alexarsh
  • 5,123
  • 12
  • 42
  • 51
  • OP is trying to make a dropdownlist using jquery ui autocomplete here – naveen Apr 28 '12 at 11:01
  • Hi Alex, thank you for your response. I was however looking for a solution that was restricted jQuery Mobile's autocomplete library (i.e. no further plugins). – Ben Pearce Apr 28 '12 at 22:58
  • Ok. So how about this one: http://www.andymatthews.net/read/2012/03/27/jQuery-Mobile-Autocomplete-now-available – alexarsh Apr 29 '12 at 08:11
0

One simple workaround will be to HTML Encode list using JavaScript after label has been assigned on success call

var elm = $("#list");
elm.html(elm.text());

Working fiddle : http://jsfiddle.net/naveen/yRwH7/

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251
0

try placing anchor tags around your image in the return html eg. ...

.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
         .data( "item.autocomplete", item )
         .append("<a><img src='"+item.value+"' alt='no photo'/></a>"+ item.label)
         .appendTo( ul );
zeeside
  • 16
  • 2
  • Actually you have to have anchor tags as your root node in your LI for custom autocomplete to function at all. – kamasheto Sep 09 '12 at 14:00