Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Need to Copy items in Select to another Select

Posted on 2009-04-04
Medium Priority
Last Modified: 2012-05-06

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

Question by:ddotson
  • 3
  • 2

Expert Comment

ID: 24069957
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


Author Comment

ID: 24071796
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.

Accepted Solution

djon2003 earned 2000 total points
ID: 24073090
Well..you 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

Author Comment

ID: 24364380
I would like to award points for participation and the pseudocode.

Author Closing Comment

ID: 31566670
Thanks for your help on this.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question