How would I, using jQuery, remove the dups
You can do it like this:
var found = [];
$("select option").each(function() {
if($.inArray(this.value, found) != -1) $(this).remove();
found.push(this.value);
});
Give it a try here, it's a simple approach, we're just keeping an array of found values, if we haven't found the value, add it to the array (.push()), if we have found the value, this one's a dupe we found earlier, .remove() it.
This only crawls the once, minimizing DOM traversal which is a lot more expensive than array operations. Also we're using $.inArray() instead of
.indexOf()
so this works properly in IE.
If you want a less efficient but shorter solution (just for illustration, use the first method!):
$('select option').each(function() {
$(this).prevAll('option[value="' + this.value + '"]').remove();
});
You can test it here, this removes all siblings with the same value, but it's much more expensive than the first method (DOM Traversal is expensive, and multiple selector engine calls here, many more). We're using .prevAll() because you can't just remove .siblings() inside an .each(), it'll cause some errors with the loop since it expected the next child.