Reputation: 8202
How do I remove duplicate names in HTML select box via jQuery. Here is the fiddle - Fiddle Demo
I tried using -
$('select option').each(function() {
$(this).prevAll('option[value="' + this.value + '"]').remove();
});
but this only removes duplicate entries when the option value
are same.
How to I remove duplicates when the text is same but option values
are different
Upvotes: 0
Views: 463
Reputation: 2857
Try this:
$('select option').each(function() {
$(this).prevAll('option[value="' + this.text + '"]').remove();
});
Upvotes: 0
Reputation: 144689
var $o = $('select option');
$o.filter(function(i, el) {
return $o.filter(function() {
return this.textContent === el.textContent;
}).index() !== i;
}).remove();
Upvotes: 0
Reputation: 1714
you can use a array to see if the value is already attribued :
label = new Object();
$('select option').each(function(){
if(label[$(this).text()] != 1)
label[$(this).text()] = 1;
else $(this).remove();
});
http://jsfiddle.net/b6fxLnv2/3/
Upvotes: 1