Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

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
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

You want to remove duplicate options from the select field ?

Avatar of jaggernat
jaggernat

ASKER

yes
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.

ok "viewsource" (html source) for "addtlFacs" field yeilds this


 <select name="addtlFacs" multiple="multiple" size="5" tabindex="24" style="width:170" class="formstyle"></select>
There are no <option>s, but anyway, what do you like to compare for same options? Option text or option values?
OK, the JSP button is something like this:

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

ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);" />



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.

Thanks for the compliment :)

wow. that was just perfect.
thanks