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
2009-09-18 07:32:37
you could put a break; when you remove the item to stop the loop
Kheu
2009-09-18 09:57:56
More efficient to use the select's selectedIndex property rather than iterate over its options
Tim Down
2009-09-18 13:18:51
Please this is not my code it is a sample from the site.
TheVillageIdiot
2009-09-18 13:26:47
+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
2009-09-18 07:36:53
A:
Doing it the jQuery way:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<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
2009-09-18 07:44:51
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
2009-09-18 13:15:46
@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
2009-09-21 08:12:36
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
2010-05-21 00:59:33