Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML in jQuery UI autocomplete

Before jQuery UI 1.8.4 I could use HTML in the JSON array I built to work with an autocomplete.

I was able to do something like:

$row_array['label'] = '<span style="color: red; font-family: courier;">User, Name</span>'; 

That would show up as red text in the drop down.

As of 1.8.4 that does not work. I found http://dev.jqueryui.com/ticket/5275 which tells me to use the custom HTML example here which I have had no luck with.

How can I go about getting HTML to show up in the suggestion?

My jQuery is:

<script type="text/javascript">     $(function() {         $("#findUserIdDisplay").autocomplete({             source: "ui_autocomplete_users_withuname.php",             minLength: 2,             select: function(event, ui) {                 $('#findUserId').val(ui.item.id);             }         });     }); </script> 

My JSON array includes HTML like the following:

[{"label":"<span style="color: red";>User, Name</span>","value":"User, Name","id":"10"}] 
like image 806
Jason Avatar asked Aug 15 '10 15:08

Jason


People also ask

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How can create autocomplete search box in jQuery?

For this, we are going to use the jQuery inbuilt function called autocomplete. We will do that in two different stages. Create a basic HTML file and add an input bar to it. Implement the autocomplete functionality.

What is the default value of Append to option of autocomplete () method?

Option - appendTo By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front. If an element with the ui-front class is found, the menu will be appended to that element.


2 Answers

Add this to your code:

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

So your code becomes:

<script type="text/javascript">  $(function () {      $("#findUserIdDisplay").autocomplete({          source: "ui_autocomplete_users_withuname.php",          minLength: 2,          select: function (event, ui) {              $('#findUserId').val(ui.item.id);              return false;          }      }).data("ui-autocomplete")._renderItem = function (ul, item) {          return $("<li></li>")              .data("item.autocomplete", item)              .append("<a>" + item.label + "</a>")              .appendTo(ul);      };  }); </script> 

Note: On old versions of jQueryUI use .data("autocomplete")" instead of .data("ui-autocomplete")

like image 134
Kieran Andrews Avatar answered Oct 12 '22 02:10

Kieran Andrews


This is also documented in jquery-ui autocomplete documentation at: http://api.jqueryui.com/autocomplete/#option-source

The trick is:

  1. Use this jQuery UI extension
  2. Then in autocomplete option pass autocomplete({ html:true});
  3. Now you can pass html &lt;div&gt;sample&lt;/div&gt; in "label" field of JSON output for autocomplete.

If you don't know how to add the plugin to JQuery UI 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.
like image 25
Wasiqul Islam Avatar answered Oct 12 '22 00:10

Wasiqul Islam