0

Hello.

I want to delete duplicate values of options tag!
this is my code.

But this is not good and have problem!

function DropDups()
{
   var i=0;
   var forml;
   forml = document.getElementsByTagName('form').length; 
   for(i=0;i<forml;i++)
   {
      var Target=document.forms[i]['color'];
      for (var avvali=1; avvali<Target.options.length; avvali++)
      {
         valavvali = Target.options[avvali].text;
         for (var dovvomi=1; dovvomi<Target.options.length; dovvomi++)
         {
            valdovvomi = Target.options[dovvomi].text;  
            if (valavvali == valdovvomi)
            {
               //alert(valavvali + '==' + valdovvomi);
               Target.remove(valdovvomi);
            }
         }
      }
   }
   //alert(document.forms[1]['color'].options.length);
}

Please help me.

Thanks.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Airshow
0

MOrteza,

Try this:

function removeDuplicateOptions(s, comparitor) {
	if(s.tagName.toUpperCase() !== 'SELECT') { return false; }
	var c, i, o=s.options, sorter={};
	if(!comparitor || typeof comparitor !== 'function') {
		comparitor = function(o) { return o.value; };//by default we comare option values.
	}
	for(i=0; i<o.length; i++) {
		c = comparitor(o[i]);
		if(sorter[c]) {
			s.removeChild(o[i]);
			i--;
		}
		else { sorter[c] = true; }
	}
	return true;
}

Example 1:

//Use this version to compare by value (defaults comparison).
var s = document.getElementById("mySelect");
removeDuplicateOptions(s);

Example 2:

//Use this version to compare by text (innerHTML).
var s = document.getElementById("mySelect");
removeDuplicateOptions(s, function(o){
	return o.innerHTML;
});

Example 3:

//Use this version to compare by value and text (innerHTML).
var s = document.getElementById("mySelect");
removeDuplicateOptions(s, function(o){
	return o.value + o.innerHTML;
});

Airshow

0

Thanks a lot.

I have a problem.
I have many forms and in that forms is select tag with "color" id.

this is true?

function removeDuplicateOptions(s, comparitor) {
	if(s.tagName.toUpperCase() !== 'SELECT') { return false; }
	var c, i, o=s.options, sorter={};
	if(!comparitor || typeof comparitor !== 'function') {
		comparitor = function(o) { return o.value; };//by default we comare option values.
	}
	for(i=0; i<o.length; i++) {
		c = comparitor(o[i]);
		if(sorter[c]) {
			s.removeChild(o[i]);
			i--;
		}
		else { sorter[c] = true; }
	}
	return true;
}
function callremoveduplicate()
{
   var i=0;
   var forml;
   forml = document.getElementsByTagName('form').length; 
   for(i=0;i<forml;i++)
   {
   var s=document.forms[i]['color'];
   //var s = document.getElementById("mySelect");
   removeDuplicateOptions(s);
   }
}

At the end of body tag is:

<script>
callremoveduplicate();
</script>
0

Ok.

Thanks a lot!

What I am doing?!?!

I edit home.php source code but when I run that I see nothing!!!
But in www directory the first run is from index.php!!!!

Means I edit home.php and run index.php!!!!

Your source code is correct!!

Thanks again.

;D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.