The following works fine in IE5.5, and should also be fine in IE5.0.
Since it's pure DOM, it should also work just fine in NN6, though I
didn't test that...
<script>
function MoveOption(x){
var y=x[x.selectedIndex], c=y.cloneNode(true);
y.parentNode.removeChild(y);
y=document.getElementById('Sel1');
// Next line positions moved option 1st...
y.insertBefore(c,y.options[0]); y.options[0].selected=true;
// Or, substitute next line to position last instead...
// y.appendChild(c); c.selected=true;
if (x.length<2) x.parentNode.removeChild(x)}
</script>
<p><select id="Sel1">
<OPTION value="cR">Red</OPTION>
<OPTION value="cY">Yellow</OPTION>
<OPTION value="cB">Blue</OPTION>
</select></p>
<p><select id="Sel2" onchange="MoveOption(this.options)">
<OPTION>Select...</OPTION>
<OPTION value="fA">Apple</OPTION>
<OPTION value="fP">Peach</OPTION>
<OPTION value="fG">Grape</OPTION>
</select></p>
- Peter
Quote:
> I am writing some javascript so that users can add , remove and sort a
> selection of items.
> The system uses a right box of selected items populated from a record which
> may be removed or sorted.
> The left box of items is populated by the remaining options not currently
> held in the record. An item may only be either in or out of the record (ie
> left or right).
> Everything works fine except for one problem with the add or remove feature:
> I can not seem to create a new option in a particular select box.
> eg removing from the right and putting on the end of the left does not work:
> selectboxleft.options[lengthofleft] = selectboxright.options[i]
> (or a modified version to replace the value and text seperately)
> But replacing an existing option does work:
> eg options[lengthofleft-1] will put the value to remove across to the left
> by replacing the last value on the left
> Does anyone know if it is actually possible to create new options once a
> select box is written?
> Many thanks
> Jon Shaw