Home > jQuery > jQuery Remove Select Tag Duplicates

jQuery Remove Select Tag Duplicates

Removing select tag duplicates using jQuery can be achieved using following code snippet

var usedDisplayNames = {};
$("select[name='tagname'] > option").each(function () {
    if(usedDisplayNames[this.text]) {
        $(this).remove();
    } else {
        usedDisplayNames[this.text] = this.value;
    }
});

usedDisplayNames is an array which will have Key equals select tag text and Value equals select tag value. The code iterates through each option tag of select and check whether the item is already present in the array or not. If present then item will be removed from select. Else item will gets added to the array.

Advertisements
Categories: jQuery
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: