Over a million developers have joined DZone.

Move Multiple Options From One Select To Another

·
// Theis code requires you include prototype.js


function copyOptions(from , to)
{
	for (var i=0; i < $(from).options.length; i++) 
	{
        if ($(from).options[i].selected) 
        {
            var optionName = new Option($(from).options[i].text, 
            	$(from).options[i].value);
            $(to).options[$(to).length] = optionName;
        }
    }
    remove($(from));
}

function remove(theSel)
{
  	var selIndex = theSel.selectedIndex;
  	if (selIndex != -1) 
  	{
	    for(i=theSel.length-1; i>=0; i--)
	    {
		      if(theSel.options[i].selected)
		      	theSel.options[i] = null;
	    }
    	if (theSel.length > 0) 
      		theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
  	}
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}