Question : Javascript removing all options not selected

Greetings All;

How can I remove all the options in a select box that are not highlighted? See code below.


If the "two" option were highlighted, and the "Remove Not Selected "button were clicked, options one and three would be removed.

Thank Yooou, in advance,  for your assistance.

´*•.¸(`*•.¸?¸.•*´)¸.•*´
?«´•°*42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
<html>
<head>
<title>Delete Unselected from Mybox</title>

</head>

<body>
<FORM name="HereWeGo">
  <select name="mybox" id="mybox" multiple>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select>
  <input type="button" value="REMOVE NOT SELECTED"/>
</form>

</body>

</html>


Answer : Javascript removing all options not selected

new one :
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
<html>
<head>
<title>Delete Unselected from Mybox</title>
<script language="javascript">
	function cleanlistbox(lb){
		var l = lb.options.length;
		for(var i=0;i<l;i++) {
			if(!lb.options[i].selected) {
				lb.remove(i);
				i--;
				l--;
			}
		}
	}
</script>
</head>

<body>
<FORM name="HereWeGo">
  <select name="mybox" id="mybox" multiple>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
  </select>
  <input type="button" value="REMOVE NOT SELECTED" onClick="cleanlistbox(document.HereWeGo.mybox)"/>
</form>

</body>

</html>
Random Solutions  
 
programming4us programming4us