• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

removing duplicates in javascript

Dear experts,

I have a field in my jsp page which is


<html:select property="addtlFacs" styleClass="formstyle" multiple="true" size="8" style="width:80">
<html:optionsCollection property="allAddtlFacs" value="value" label="label"/>
</html:select>

<a href="javascript:dedupe_list('addtlFacs');">Remove duplicates</a>

and i have a link "Remove Duplicates" below that field which removes duplicates.the method passes
the property "addtlFacs" as parameter . I am able to get that property in my javascript file,but
unable to remove duplicates from the field. I tried to use a bunch of javascript functions which i
found online but no luck.

any help would be greatly appreciated.

thanks
J
0
jaggernat
Asked:
jaggernat
  • 5
  • 3
  • 3
  • +1
1 Solution
 
Pravin AsarPrincipal Systems EngineerCommented:
You want to remove duplicate options from the select field ?

0
 
jaggernatAuthor Commented:
yes
0
 
Dennis MaederCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ZvonkoSystems architectCommented:
It would be good to have a button that would call that function.
Perhaps like this:

<html:select property="addtlFacs" styleClass="formstyle" multiple="true" size="8" style="width:80">
<html:optionsCollection property="allAddtlFacs" value="value" label="label"/>
</html:select>
</html:button onClick="dedupe_list(this,'addtlFacs');" label="Remove duplicates" /a>

Let the html be generated for that page and copy that html source snippet to EE for furter coding.

0
 
jaggernatAuthor Commented:
ok "viewsource" (html source) for "addtlFacs" field yeilds this


 <select name="addtlFacs" multiple="multiple" size="5" tabindex="24" style="width:170" class="formstyle"></select>
0
 
Dennis MaederCommented:
Shouldn't this be on the JSP page?
http://www.experts-exchange.com/Web/Web_Languages/JSP/
D
0
 
ZvonkoSystems architectCommented:
There are no <option>s, but anyway, what do you like to compare for same options? Option text or option values?
0
 
ZvonkoSystems architectCommented:
OK, the JSP button is something like this:

<HTML:button property="removeDuplicates" value="Remove duplicates" styleClass="buttonNew" onclick="dedupe_list(this,'addtlFacs');" />

0
 
Pravin AsarPrincipal Systems EngineerCommented:
Here is a simple , compact function to serve the task at hand.

<html>
<head>
<title>Remove Duplicate Form Select List</title>
</head>
<body>
<script language="javascript">
function removeOldSchool(theSel)
{
    var del = new Array();
      dx=0;
      // Gather the duplicates
      for (i = 0; i < theSel.options.length; i++) {
            curOpt = theSel.options[i];
            for (j=theSel.length-1; j > i; j--) {
                  chkOpt = theSel.options[j];
                  if (chkOpt.value == curOpt.value) {
                        del[dx++] = j;                        
                        break;
                  }
            }            
      }
      if (!dx) { return; }
      // remove
      for (i = del.length -1; i > -1; i--) {
            theSel.options[del[i]] = null;
      }
}
</script>
<form>
<select name="sel1" size="10" style="width:200px;">
      <option value="value1">Value 1</option>
      <option value="value2">Value 2</option>
      <option value="value3">Value 3</option>
      <option value="value4">Value 4</option>
      <option value="value1">Value 1</option>
      <option value="value5">Value 5</option>
      <option value="value2">Value 2</option>
</select>
<input type="button" value="remove duplicate" onclick="removeOldSchool(this.form.sel1);">
</form>

</body>
</html>
0
 
ZvonkoSystems architectCommented:
My is more simple (or less complicated :)

<script>
function dedupe_list(theSel){
  var unq = new Array();
  var opt = theSel.options;
  for(var i=0;i<opt.length;i++){
    unq["_"+opt[i].value] = opt[i].text;
  }
  opt.length=0;
  for(v in unq){
    opt[opt.length] = new Option(unq[v], v.substr(1));
  }
}
</script>

And the button is simelar to above propsed:
<HTML:button property="removeDuplicates" value="Remove duplicates" styleClass="buttonNew" onclick="dedupe_list(this.form.addtlFacs);" />



0
 
ZvonkoSystems architectCommented:
Sorry, now I see. My version is NOT less complicated. Your is even better because it does not recreate the Options like my version does.

0
 
Pravin AsarPrincipal Systems EngineerCommented:
Thanks for the compliment :)

0
 
jaggernatAuthor Commented:
wow. that was just perfect.
thanks
0
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now