Recently I have come across a situation where I need to customize the UI of jQuery UI autocomplete plugin. For reference I am adding the plugin resource as below-
http://jqueryui.com/demos/autocomplete/
Now if we look into the dynamic HTML generated while typing the text in the input box we can find HTML of the form-
<input class="ui-autocomplete-input"/>
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all">
<li class="ui-menu-item">
<a class="ui-corner-all">item 1</a>
</li>
<li class="ui-menu-item">
<a class="ui-corner-all">item 2</a>
</li>
<li class="ui-menu-item">
<a class="ui-corner-all">item 3</a>
</li>
</ul>
Changing the UI is nothing the CSS classes attached in dynamic HTML for the autocomplete. If we look in the css file of the autocomplete plugin we can find few more classes. I am not great at designing but just tried to overwrote the class of the plugin. Code goes here-
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<style>
.ui-corner-all
{
-moz-border-radius: 4px 4px 4px 4px;
}
.ui-widget-content
{
border: 5px solid black;
color: #222222;
background-color: Red;
}
.ui-widget
{
font-family: Verdana,Arial,sans-serif;
font-size: 15px;
}
.ui-menu
{
display: block;
float: left;
list-style: none outside none;
margin: 0;
padding: 2px;
}
.ui-autocomplete
{
cursor: default;
position: absolute;
}
.ui-menu .ui-menu-item
{
clear: left;
float: left;
margin: 0;
padding: 0;
width: 100%;
}
.ui-menu .ui-menu-item a
{
display: block;
padding: 3px 3px 3px 3px;
text-decoration: none;
cursor: pointer;
background-color: Green;
}
.ui-menu .ui-menu-item a:hover
{
display: block;
padding: 3px 3px 3px 3px;
text-decoration: none;
color: White;
cursor: pointer;
background-color: ButtonText;
}
.ui-widget-content a
{
color: #222222;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#autocomplete").autocomplete({
source: function(request, response) {
response($.map(dataArray, function(item) {
if (item.label.indexOf($("#autocomplete").val()) == 0) {
return {
label: item.label,
value: item.value
}
}
}))
},
minLength: 1
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="autocomplete" runat="server" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
var obj = (new[] { new { value = "1", label = "abc" } }).ToList();
obj.Add(new { value = "1", label = "abc" });
obj.Add(new { value = "2", label = "acbc" });
obj.Add(new { value = "3", label = "abcfd" });
obj.Add(new { value = "4", label = "assbc" });
obj.Add(new { value = "5", label = "aaabc" });
obj.Add(new { value = "6", label = "ddabc" });
obj.Add(new { value = "7", label = "dggabc" });
obj.Add(new { value = "8", label = "dabc" });
obj.Add(new { value = "9", label = "vvabc" });
obj.Add(new { value = "10", label = "vabc" });
obj.Add(new { value = "11", label = "vgafftbc" });
obj.Add(new { value = "12", label = "vabc" });
obj.Add(new { value = "13", label = "vddabc" });
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
string data = jss.Serialize(obj);
ClientScript.RegisterClientScriptBlock(Page.GetType(), "scr", " var dataArray=" + data, true);
}