Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
Can anyone tell me how can I filter dropdown with unique values, rite now i've this kind of values in my dropdown:
san
san
san
vert
vert
vert
pref
pref
pref


I want this :
san
vert
pref


I populated my dropdown from parsing XML.
Posted

You can use jQuery's .unique .
e.g.
JavaScript
$.unique(["a", "b", "c", "d", "a"]);

will return a,b,c,d.

More info: http://api.jquery.com/jQuery.unique/
 
Share this answer
 
Hi,
You can try below-

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
<script type="text/javascript">
    var ddlValues = ["san", "san", "san", "vert", "vert", "vert", "pref", "pref", "pref"];
    var arrDistinct = new Array();
    $(document).ready(function () {
        $(ddlValues).each(function (index, item) {
            if ($.inArray(item, arrDistinct) == -1)
                arrDistinct.push(item);
        });
        debugger;
        $(arrDistinct).each(function (index, item) {
            $("#myDDL").append($("<option>").html(item));
        });

        //Using unique
        $($.unique(arrDistinct)).each(function (index, item) {
            $("#other").append($("<option>").html(item));
        });
    });
</script>
</head>
<body>
    <form id="form1"  runat="server">
    <select id="myDDL" ></select>
    <br />
    <select id="other"></select>
    </form>
</body>
</html>
 
Share this answer
 
v3
Comments
[no name] 15-Sep-11 8:19am    
Thanks a lot sir, it helped me a lot.

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