Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display all the items in the dropdown list with starting string.
For example if I type app in textbox it must display all the items starting with letter "app". Like this: Apple

I don't want to filter like this:

Apple
Pineapple

Please Help me....

It's very urgent

Here is my code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Search and Filter a DropDownList</title>
    <script src="Scripts/jquery-1.3.2.min.js">
        type="text/javascript"&gt;</script>
    
    <script type="text/javascript">
        $(function() {
            var $txt = $('input[id$=txtNew]');
            var $ddl = $('select[id$=DDL]');
            var $items = $('select[id$=DDL] option');
            $txt.keyup(function() {
                searchDdl($txt.val());
            });
            function searchDdl(item) {
                $ddl.empty();
                var exp = new RegExp(item, "i");
                var arr = $.grep($items,
                    function(n) {
                        return exp.test($(n).text());
                    });
                if (arr.length &gt; 0) {
                    countItemsFound(arr.length);
                    $.each(arr, function() {
                        $ddl.append(this);
                        $ddl.get(0).selectedIndex = 0;
                    }
                    );
                }
                else {
                    countItemsFound(arr.length);
                    $ddl.append("<option>No Items Found</option>");
                }
            }
            function countItemsFound(num) {
                $("#para").empty();
                if ($txt.val().length) {
                    $("#para").html(num + " items found");
                }
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
                <asp:textbox id="txtNew" runat="server" xmlns:asp="#unknown">
            ToolTip="Enter Text Here"&gt;</asp:textbox><br />
        <asp:dropdownlist id="DDL" runat="server" xmlns:asp="#unknown">
            <asp:listitem text="Apple" value="1"></asp:listitem>
            <asp:listitem text="Orange" value="2"></asp:listitem>
            <asp:listitem text="Peache" value="3"></asp:listitem>
            <asp:listitem text="Banana" value="4"></asp:listitem>
            <asp:listitem text="Melon" value="5"></asp:listitem>
            <asp:listitem text="Lemon" value="6"></asp:listitem>
            <asp:listitem text="Pineapple" value="7"></asp:listitem>
            <asp:listitem text="Pomegranate" value="8"></asp:listitem>
            <asp:listitem text="Mulberry" value="9"></asp:listitem>
            <asp:listitem text="Apricot" value="10"></asp:listitem>
            <asp:listitem text="Cherry" value="11"></asp:listitem>
            <asp:listitem text="Blackberry" value="12"></asp:listitem>
        </asp:dropdownlist>
            </div>
    </form>
</body>
</html>
Posted
Updated 5-Sep-10 7:40am
v2
Comments
Sandeep Mewara 5-Sep-10 13:41pm    
Use PRE tags to format your code part. It makes the question readable.

1 solution

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900