Remove all the items in a drop down list except the first using Jquery

I needed a drop down list to be populated from a selection in another drop down list. Each select should change the options listed in the second list. Using Jquery it was easy to remove all the options and rebuild the list, but I wanted to keep the first option. I finally found the solution searching the Jquery site and it’s quite simple.

selection.children('option:not(:first)').remove();

Where selection is the id of the drop down list. I hope this helps somebody not spend the hour it took me to figure it out.

Advertisements

5 thoughts on “Remove all the items in a drop down list except the first using Jquery

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