Using .siblings()
(to target sibling option
elements), and Attribute Equals Selector [attr=""]
$(".select option").each(function() {
$(this).siblings('[value="'+ this.value +'"]').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>
(works also for multiple .select
on the same page)
I added a class .select
to the <select>
element to be more selector-specific
How it works:
while option
s are accessed one by one (by .val()
) – lookup for .sibling()
option
s that have the same "[value=""+ this.value +""]"
and .remove()
them.