2

I have a multi select dropdown list. I can get the array of selected values using:

selectedItems = $("#myList").val(); // works.

Now, how can I remove the selected items from the dropdown list?

john
  • 2,733
  • 6
  • 23
  • 14

3 Answers3

12
$("#myList option:selected").remove();

will work.


Edit: I misunderstood the comment, but I will leave it as an example for removing certain elements in general.
If you want to remove the elements based on the value in the array, you have to loop over the array:

var $list = $("#myList"),
    toRemove = $();

for(var i = selectedItems.length; i--;) {
   toRemove = toRemove.add($list.find('option[value="' + selectedItems[i] + '"]'));
}
toRemove.remove();

DEMO

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • thanks, say I already have a variable pointing to my list: var myList = $("#myList"); how do I use this variable to do what you suggested? – john Apr 25 '11 at 12:21
  • Actually, I want to use the first way you suggested. My only question was that I already have a variable pointing to the select element; so i don't know how to apply the "option:selected" to it. – john Apr 25 '11 at 12:30
  • @john: Oh sorry, I misunderstood that. You can use `find` then: `myList.find('option:selected').remove()`. – Felix Kling Apr 25 '11 at 12:31
  • @john: Or `children`: `myList.children('option:selected').remove()` – Felix Kling Apr 25 '11 at 12:39
2

This could help you:- Remove Selected Option using jQuery

Community
  • 1
  • 1
Misam
  • 4,320
  • 2
  • 25
  • 43
2
$("[Id$='ddlShowRun'] option:selected").remove();
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Genius
  • 21
  • 1