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"}]
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.
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.
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.
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")
This is also documented in jquery-ui autocomplete documentation at: http://api.jqueryui.com/autocomplete/#option-source
The trick is:
autocomplete({ html:true});
<div>sample</div>
in "label" field of JSON output for autocomplete.If you don't know how to add the plugin to JQuery UI then:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With