Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListBox: Remove duplicate entries via javascript

Posted on 2004-11-17
4
Medium Priority
?
2,356 Views
Last Modified: 2008-01-09
Hello

I have a list box document.form1.customers that is populated dynamically.

I am looking for a JS function that will loop through all the entries and remove the duplicates, leaving me with just unique entries.

I am using Javascript to populate the box, hence the need for a Javascript function to remove the duplicates - it isn't just a case of using SELECT DISTINCT.....

Example below

Many thanks, Daz

e.g.

BOB
BOB
BOB
KATE
KATE
DAN
FRED

will become

DAN
BOB
KATE
DAN
FRED

Thanks again

Daz
0
Comment
Question by:daz_oldham
  • 3
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 12604656
<HTML>
<HEAD>
<SCRIPT language="javascript">

function removeDupes(listbox)
{
   if ( listbox.options.length > 0 )
      for ( var i=1,n=listbox.options.length; i<n; i++ )
         if ( listbox.options[i].value == listbox.options[i-1].value )
         {
             listbox.options[i] = null;
             n--
         }
}

</SCRIPT>
</HEAD>

<BODY>
<FORM name="form1">

<SELECT name="customers">
<OPTION value="BOB">BOB</OPTION>
<OPTION value="BOB">BOB</OPTION>
<OPTION value="BOB">BOB</OPTION>
<OPTION value="KATE">KATE</OPTION>
<OPTION value="KATE">KATE</OPTION>
<OPTION value="DAN">DAN</OPTION>
<OPTION value="FRED">FRED</OPTION>
</SELECT>

<INPUT type="button" value="De-Dupe" onclick="removeDupes(document.form1.customers)" />

</FORM>
</BODY>
</HTML>

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12604683
oops, slight change ... add i--


         if ( listbox.options[i].value == listbox.options[i-1].value )
         {
             listbox.options[i] = null;
             n--;
             i--;
         }
0
 
LVL 3

Expert Comment

by:Tyzer
ID: 12604805

Try this :
<script>
      function fnSortDropDown(){
            var oSelect = document.getElementById('myselect')
            var i = 0;
            while(i<oSelect.options.length){
                  fnRemoveOptions(oSelect.options[i].value)
                  i++;
            }
      }
      
      function fnRemoveOptions(sValue){
            var oSelect = document.getElementById('myselect');
            var i = oSelect.options.length;
            var bFound = false;
            while(i>0){
                  i--;
                  if (oSelect.options[i].value==sValue && bFound==false) {
                        bFound = true
                  }
                  else if (oSelect.options[i].value==sValue){
                        oSelect.removeChild(oSelect.options[i]);
                  }
                  
            }
      }
</script>

<select id=myselect>
      <option value="bob">bob</option>
      <option value="bob">bob</option>
      <option value="rick">rick</option>
      <option value="hannah">hannah</option>
      <option value="hannah">hannah</option>
      <option value="bob">bob</option>
      <option value="bob">bob</option>
      <option value="hannah">hannah</option>
      <option value="bob">bob</option>
      <option value="rick">rick</option>
      <option value="bob">bob</option>
      <option value="bob">bob</option>
</select>

<P>

<input type=submit value="sort" onclick="javascript:fnSortDropDown()">
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12604838
also, use  .text  instead of  .value  if the associated values are different from the name, e.g.

<OPTION value="123">BOB</OPTION>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question