views:

340

answers:

4

How can we remove a selected item from SELECT whose SIZE is not declared, means acting as drop down list. using javascript

+1  A: 

Here is a very nice article outlining how you can add/remove items to select list.

This is function to remove selected item (from site):

function removeOptionSelected()
{
  var elSel = document.getElementById('selectX');//selectX IS ID OF SELECT
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
      break;//As suggested in comments
    }
  }
}

THIS IS WHAT I WOULD HAVE DONE:

function RemoveOption(){
     $("#SelectId option:selected").remove();
}
TheVillageIdiot
you could put a break; when you remove the item to stop the loop
Kheu
More efficient to use the select's selectedIndex property rather than iterate over its options
Tim Down
Please this is not my code it is a sample from the site.
TheVillageIdiot
+2  A: 

Since you want to delete a single selected option, you could easily:

Remove the element using the select.remove function:

var el = document.getElementById('selectId');
el.remove(el.selectedIndex);

Or by DOM manipulation:

var el = document.getElementById('selectId');
el.removeChild(el.options[el.selectedIndex]);

Check an example here.

CMS
A: 

Doing it the jQuery way:

<html>
<head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(document).ready(function(){
                $("#myButton").click(function(){
                    $("#mySelect option:selected").remove();
                });
            });
        </script>
</head>
<body>
    <select id="mySelect">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
    </select>

    <input type="button" id="myButton" value="remove"/>
</body>
</html>

I know, nobody asked for jQuery, but IMHO nobody should DOM+JavaScript without jQuery anymore!

Tim Büthe
jQuery is huge overkill for something this simple, and using it discourages people from actually learning anything except how to write jQuery code.
Tim Down
@Tim Down: I disagree. What's bad about learning jQuery? In my opinion, it's totally okay to learn jQuery and JavaScript but not the DOM in detail. Is like writing Java without learning the underlying byte code, totally okay. Or maybe learning a language with garbage collection and not go through all the memory stuff in detail. Beside this, the overhead is not that big if you simply link the script from a common place like google code or jquery.com the user will most likely already have a cached version.
Tim Büthe
A: 
function deleteFromSelect(list){
  var i = 0;
  var limit = list.options.length;
  while(i < limit)  
  {
      if(!list.options[i].selected)
     {
         i++;
     }
     else
    {
        list.options[i] = null;
        limit--;
    }
  }
}

Reference: http://abreakorcontinue.blogspot.com/2010/05/how-to-delete-all-selected-elements-on.html

Pablo Fonseca