Need to Copy items in Select to another Select


I have a project where I have two select boxes side by side.  I am able to move items from one select to another using the code below.   I didn't write this code.  Once the end user moves the desired items from the first box to the second, I need them to be able to use a drop down box below the two select boxes to make a choice.  The choices in the drop down box come from the second select box.  So, if I have Red, Green, Blue, Yellow and Black in the first box, and the user moves Red, Green, and Blue over to the second box, then the drop down box needs to be populated with Red, Green, and Blue.  They will be able to select just one item from that drop down box.

Again, the code to move items from one select box to another works fine.  I just need to modify it so that it will also populate the drop down box.

Let me know if I've left out some details.


function MoveItems(lstbxFrom,lstbxTo)
			var list = document.getElementById("selectedItems");
			var newOrder = document.getElementById("newOrder");
			var varFromBox = document.all(lstbxFrom);
			var varToBox = document.all(lstbxTo);
			if ((varFromBox != null) && (varToBox != null)) 
				if(varFromBox.length < 1) 
						alert('There are no items in the source ListBox');
						return false;
				if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
					alert('Please select an Item to move');
					return false;
				while ( varFromBox.options.selectedIndex >= 0 ) 
					var newOption = new Option(); // Create a new instance of ListItem 
					newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
					newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
					varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
					varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 
			var s = "";
			for( i=0; i<list.options.length; i++ )
				s += list.options[i].value + "|";
			s = s.substring(0,s.length-1);
			newOrder.value = s;
			return false; 
Here's the HTML:
<select size="15" name="allItems" id="allItems" style="width:200px;width:200px">
<option value='red'>Red<option>
<option value='green'>Green<option>
<option value='Blue'>Blue<option>
<option value='Yellow>Yellow<option>
<option value='Black'>Black<option>
<input type = "button" style="width: 25px;" id = "btnMoveLeft" onclick = "MoveItems('selectedItems','allItems')" value="<-">
<input type = "button" style="width: 25px;" id = "btnMoveRight" onclick = "MoveItems('allItems','selectedItems')" value="->">
<select size="15" name="selectedItems" id="selectedItems" style="width: 200px;"></select>
Here's the drop down box:
Sort Order:<select name="sortOrder" id="sortOrder" size="15" style="width: 200px;"></select>

Open in new window

Who is Participating?
djon2003Connect With a Mentor Commented: are right.. The function won't fit your need then..

Here is a pseudocode :
function MoveItems(mainList, toLists, reverse) {
if (reverse == "undefined") {reverse = false;}

if (reverse == true) {
 COPY from first toList to mainList..
 DELETE from toLists  --> Loop/Iterate through the array
else {
 COPY to toLists  --> Loop/Iterate through the array
 DELETE from mainList

Calling the function :
MoveItems('allItems', ['selectedItems','sortOrder']); // From allItems to others
MoveItems('allItems', ['selectedItems','sortOrder'], true); // From others to allItems
This is maybe not the "perfect" code. But it should do it without any modifications to your function.
<input type = "button" style="width: 25px;" id = "btnMoveLeft" onclick = "MoveItems('selectedItems','allItems');MoveItems('sortOrder','allItems');" value="<-">
<input type = "button" style="width: 25px;" id = "btnMoveRight" onclick = "MoveItems('allItems','selectedItems');MoveItems('allItems','sortOrder');" value="->">

Open in new window

ddotsonAuthor Commented:
This triggers the error checking, because when you call the function the second time, there is nothing selected...

I don't mind modifying the function.
ddotsonAuthor Commented:
I would like to award points for participation and the pseudocode.
ddotsonAuthor Commented:
Thanks for your help on this.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.