faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

101 of 184 people (55%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I copy or move all "selected" options from one select to another?

Dec 2nd, 2004 15:48
test test, B Kez, Simon Wex, Craig Smith, Bill Wyza,


This function will allow users to move items back and forth between two 
multi select boxes.

Here is where I call the function...

<p><input type="button" name="add" value=">>" onclick="mover
('add');"></p>
<p><input type="button" name="remove" value="<<" onclick="mover
('remove');"></P>

Here is the function...

function mover(move){
	if(move == 'remove')
	{
		for(x = 0;x<(document.manMod.inBox.length);x++)
		{
			if(document.manMod.inBox.options[x].selected)
			{
				with(document.manMod.outBox)
				{
					options[options.length] = new 
Option(document.manMod.inBox.options
[x].text,document.manMod.inBox.options[x].value);
				}
				document.manMod.inBox.options[x] = null;
				x = -1;
			}
		}
	}
	if(move == 'add')
	{
		
		for(x = 0;x<(document.manMod.outBox.length);x++)
		{
			if(document.manMod.outBox.options[x].selected)
			{
				with(document.manMod.inBox)
				{
					options[options.length] = new 
Option(document.manMod.outBox.options
[x].text,document.manMod.outBox.options[x].value);
				}
				document.manMod.outBox.options[x] = 
null;
				x = -1;
			}
		}
	}
	return true;
}

NOTE TO HELP THE BEGINNER JAVASCRIPT PROGRAMMERS:
Replace all the words manMod with the name of the form, replace outBox with the box to 
remove the entry from and inbox with the box the put the entry into when you click ADD. 
Then it will work for you. Great script. Took me some trying out to find out which one was 
which so I thought I would post that to save everyone else some time.