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
	}	
}

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.