How to move items in between two select lists using javascript

Move options between two multi-select fields


function fnMoveItems(lstbxFrom, lstbxTo) {
     var varFromBox = document.getElementById(lstbxFrom);
     var varToBox = document.getElementById(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 
         }
     }
     return false;
 }

 function fnMoveAllItems(lstbxFrom, lstbxTo) {
     var varFromBox = document.getElementById(lstbxFrom);
     var varToBox = document.getElementById(lstbxTo);
     if ((varFromBox != null) && (varToBox != null)) {
         if (varFromBox.length < 1) {
             alert('There are no items in the source listbox.');
             return false;
         }
         for (var i = 0; i < varFromBox.length; i++) {
             var newOption = new Option(); // Create a new instance of ListItem 
             newOption.text = varFromBox.options[i].text;
             newOption.value = varFromBox.options[i].value;
             varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox                          
         }
         while (varFromBox.length > 0) {
             varFromBox.remove(0); //Remove the item from Source Listbox 
         }
     }
     return false;
 }
How to move items in between two select lists using javascript How to move items in between two select lists using javascript Reviewed by Bhaumik Patel on 8:05 PM Rating: 5