Jquery autocomplete - custom html for result listing

心不动则不痛 提交于 2019-11-30 00:06:22

You need to replace the _renderItem method (for the autocomplete in question):

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

(assuming the items in your source have a property called customattribute)

As shown in this example: http://jqueryui.com/demos/autocomplete/#custom-data

Wasiq

This is also documented in jquery-ui autocomplete documentation at: Jquery-autocomplete.

The trick is:

  1. Use this jquery extension: github Code
  2. Then in autocomplete option pass

    html:true
    
    ...autocomplete({
    ...
    html:true
    ...
    }
    

    );

  3. Now you can pass html(like <div>sample</div>) in "label" field of JSON output for autocomplete.

If you don't know how to add the plugin to JQuery then:

  1. Save the plugin(Scott González' html extension) in a js file or download the js file.
  2. You have already added the jquery-ui autocomplete script in your html page(or the jquery-ui js file). Add this plugin script after that autocomplete javascript file.

Post date: 28th July, 2013

You could try add the attributes with the event "open":

$( ".selector" ).autocomplete({
    open: function(event, ui) {
        var jArrEl = $("a.ui-corner-all");
        jArrEl.addClass("myclass");
        jArrEl.attr("customattribute","something");
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!