Click here to Skip to main content
15,879,184 members
Articles / Web Development / HTML
Article

jQuery autocomplete custom styling

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Oct 2013CPOL2 min read 88.4K   3  
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

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --