How to remove duplicate dropdowns with the same value

How to remove duplicate values ​​→ option dropdowns?
I have the following HTML:

<option value="">All Servers</option>
<option value="com">http://smiles.com</option>
<option value="in">http://3smiles.com</option>
<option value="com">http://desk.com</option>
<option value="in">http://france24.com</option>

from the above, I have to remove duplicate values comand in, therefore, my expected result should look like this:

<option value="">All Servers</option>
<option value="com">http://smiles.com</option>
<option value="in">http://3smiles.com</option>

How to do it using jQuery?

+7
source share
8 answers

Use .siblings()(to target sibling elements option) and equal attribute selector[attr=""]

$(".select option").val(function(idx, val) {
  $(this).siblings('[value="'+ val +'"]').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="select">
  <option value="">All</option>
  <option value="com">.com 1</option>
  <option value="net">.net 1</option>
  <option value="com">.com 2</option> <!-- will be removed since value is duplicate -->
  <option value="net">.net 2</option> <!-- will be removed since value is duplicate -->
</select>
Run codeHide result

(also works for several .selecton one page)
I added a class [TG46] to the [TG47] element to be more selector-specific

:
option ( .val()) - .sibling() option "[value='"+ this.value +"']" .remove() .

+28

, ....

var map = {};
$('select option').each(function () {
    if (map[this.value]) {
        $(this).remove()
    }
    map[this.value] = true;
})

: Fiddle

+10

:

$(document).ready(function () {
    var usedNames = {};
    $("select > option").each(function () {
        if (usedNames[this.value]) {
            $(this).remove();
        } else {
            usedNames[this.value] = this.text;
        }
    });
});

: http://jsfiddle.net/aelor/aspMT/

+6
source

HTML

<select class="something">
<option value="">All Servers</option>
<option value="com">http://smiles.com</option>
<option value="in">http://3smiles.com</option>
<option value="com">http://desk.com</option>
<option value="in">http://france24.com</option>
</select>

JQuery

var seen = {};
jQuery('.something').children().each(function() {
    var txt = jQuery(this).attr('value');
    if (seen[txt]) {
        jQuery(this).remove();
    } else {
        seen[txt] = true;
    }
});

+2
source

How about this one.

DEMO: http://jsfiddle.net/naokiota/yayTm/2/

var exist = {};
$('select > option').each(function() {
    if (exist[$(this).val()]){
        $(this).remove();
    }else{
        exist[$(this).val()] = true;
    }
});

Hope this helps.

+1
source

Not tested, but something like this should work

var values = new Array();
$('#YourSelect').children('option').each(function() {
  var text = $(this).text();
  if (values.indexOf(text) === -1) {
    values.push(text);
  } else {
    //  Its a duplicate
    $(this).remove()
  }
}
+1
source

# This is another quick iteration option that counts duplicates and removes when there are more than one #

var opt = $("select[title='Country'] option");              
$.each(opt, function (key, value) {                 
  var len = $("select[title='Country'] option:contains(" + value.text + ")").length;
  if (len > 1)                   
     {
     value.remove();
     }   
  });  
0
source

You can use the following code to remove parameters with duplicate values.

    $(document).ready( function(){
        var a = new Array();
        $("#selectID").children("option").each(function(x){
            test = false;
            b = a[x] = $(this).val();
            for (i=0;i<a.length-1;i++){
                if (b ==a[i]) test =true;
            }
            if (test) $(this).remove();
        })
    });
0
source

Source: https://habr.com/ru/post/1541060/


All Articles