Jump to content

Adding and removing items from a select box. works ones then fails


garethhall

Recommended Posts

Hello, I have this function that runs when you click a button.

This functions take the selected items from box A adds it to box B and removes it from box A. Now this works ok but, as soon as you do a second selection then the remove from box A don't work. Any help would be great :)

 

 

 

function addExt(){
	var toRemExt = new Array(); //Create array to hold items to remove	
	elm = document.getElementById('ext');
	for(e = 0; e < elm.length; e++){ //loop all the select box items
		if(elm.options[e].selected){ //work with only the selected items
			addOption(document.frmExt.curExt, elm.options[e].value, elm.options[e].value); //Add option approved side
			toRemExt[e] = elm.options[e].value; //write item value to the array
		}
		/*** If at the end off the loop remove the items matching the array ***/
		if(e == (tExt-1)){
			for(a = 0; a < toRemExt.length; a++){ //loop throught the array
				for(f = 0; f < elm.length; f++){ //for each item in the array loop through the items in the select box
					if(toRemExt[a] == elm.options[f].value){ //if items match then
						removeOption(document.frmExt.ext,f); //remove item from select box
					}
				}
			}

		} //endif
	}	
}

Why not just do

 

function addExt(){
      var toRemExt = new Array(); //Create array to hold items to remove   
      elm = document.getElementById('ext');
      for(e = 0; e < elm.length; e++){ //loop all the select box items
         if(elm.options[e].selected){ //work with only the selected items
            addOption(document.frmExt.curExt, elm.options[e].value, elm.options[e].value); //Add option approved side
            removeOption(document.frmExt.ext,e); //remove item from select box
         }
      }   
   }
[code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.