ListBox: Remove duplicate entries via javascript

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
daz_oldhamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
<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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
knightEknightCommented:
oops, slight change ... add i--


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

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
knightEknightCommented:
also, use  .text  instead of  .value  if the associated values are different from the name, e.g.

<OPTION value="123">BOB</OPTION>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.