Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move to second select onchange/onclick

Status
Not open for further replies.

PBE3

Programmer
Aug 16, 2007
25
US
Hello all! I have seen posts for moving/adding options from a select box with buttons, but I am working on a script to allow options to be moved/removed onclick of option. Below is my code. My problem is after 2 options have been selected and placed into the second select box (saved options) everytime an option is clicked after that it doubles...

Please help, thanks in advanced.

Kudos!!

function savedAccess(){
savLen=document.add['saved[]'].length;
storeLen=document.add['store[]'].length;
//alert(savLen);
sel1=document.add['store[]'][document.add['store[]'].selectedIndex].value;
//alert(sel1);
for(j=0; j<storeLen; j++){
if(document.add['store[]'].options[j].selected){
alert(document.add['store[]'].options[j].value);
if(savLen==0){
s=sel1.split("|");
document.add['saved[]'].options[document.add['saved[]'].options.length]=new Option(s[2], sel1);
}else if(savLen>0){
for(i=0; i<savLen; i++){
// alert(document.add['store[]'].options[j].value+'=='+document.add['saved[]'].value);
if(document.add['store[]'].options[j].value==document.add['saved[]'].value){
document.add['saved[]'].remove(document.add['saved[]'].options.selectedIndex);

}

if(document.add['store[]'].options[j].value!=document.add['saved[]'].value){
s=sel1.split("|");
document.add['saved[]'].options[document.add['saved[]'].options.length]=new Option(s[2], sel1);
}
}
}
}
}
}
 
But you have a sacredly entangled logic and referencing with multiple error. Try this see if it is what you meant.
[tt]
function savedAccess(){
var savLen=document.add['saved[]'].length;
var storeLen=document.add['store[]'].length;
//alert(savLen);
var sel1=document.add['store[]'].value; //sel1:sel-one
//alert(sel1);
for(var i=savLen-1;i>-1;i--){
if(document.add['saved[]'].options.value==sel1){
document.add['saved[]'].remove(document.add['saved[]'].options);
}
}
var s=sel1.split("|");
document.add['saved[]'].options[document.add['saved[]'].length]=new Option(s[2], sel1);
}
[/tt]
 
Thanks...that works great. Just one thing is that if something that is already in the second select box is selected again it clears the entire second box instead of just removing that one option. I'll repost the finish product.

Thanks! :)
 
If you want to remove the entire second select box if it is discovered that the same has already chosen, you just replace this line.
[tt]
if(document.add['saved[]'].options.value==sel1){
//document.add['saved[]'].remove(document.add['saved[]'].options);
[blue]document.add['saved[]'].length=0;
break;[/blue]
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top