/    Sign up×
Community /Pin to ProfileBookmark

moving values from one select box to the other using javascript without duplicacy

Hi sir iam trying to move items from one select box to the other select box with out any duplicacy using javascript only. here i can move the selected items from one select box to the other one without any duplicacy bt i couldn’t move all the items b/w the two select boxes witout duplicacy…here is my code below and any one please tell me where it went wrong…

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
<title></title>
<script type=”text/javascript”>
function transferOptions(){
var oSel1 = document.getElementById(‘sel1’);
var oSel2 = document.getElementById(‘sel2’);
var selectedIndexes = getSelectedIndexes(oSel1);
var optionAlreadyExists;
//transfer the selected options to sel2
for(i=0; i < selectedIndexes.length; i++){
optionAlreadyExists = false;
for(j=0; j < oSel2.options.length; j++){ //check if option already exists in sel2
if(oSel1.options[selectedIndexes[i]].value == oSel2.options[j].value){
optionAlreadyExists = true;
j = oSel2.options.length;
}
}
if(!optionAlreadyExists){ //add the new option
var newOpt = new Option(oSel1.options[selectedIndexes[i]].innerHTML,oSel1.options[selectedIndexes[i]].value,false,false);
oSel2.options[oSel2.options.length] = newOpt;

}
}
}
function getSelectedIndexes(oSel){
var selectedIndexes = new Array();
for(i=0; i < oSel.options.length; i++){
if(oSel.options[i].selected){
selectedIndexes.push(i);
}
}
return selectedIndexes;
}
window.onload=function(){
document.getElementById(‘btnTransferOpts’).onclick=transferOptions;
}

// REMOVE **********************************************************

function removeOptions(){
alert(“Hi”);
var oSelr1 = document.getElementById(‘sel1’);
var oSelr2 = document.getElementById(‘sel2’);
var selectedIndexes1 = getSelectedIndexes1(oSelr2);
//alert(selectedIndexes1);
var optionAlreadyExists;
//transfer the selected options to sel1
for(i=0; i < selectedIndexes1.length; i++){
optionAlreadyExists = false;

for(j=0; j < oSelr1.options.length; j++){ //check if option already exists in sel1
//alert(oSelr2.options[selectedIndexes1[i]].value);
//alert(oSelr1.options[j].value);
if(oSelr2.options[selectedIndexes1[i]].value == oSelr1.options[j].value){
optionAlreadyExists = true;
j = oSelr1.options.length;
}
}
//alert(optionAlreadyExists);
if(!optionAlreadyExists){ //add the new option
alert(oSelr2.options[selectedIndexes1[i]].value);
var newOpt1 = new Option(oSelr2.options[selectedIndexes1[i]].innerHTML,oSelr2.options[selectedIndexes1[i]].value,false,false);
//alert(newOpt1);
oSelr1.options[oSelr1.options.length] = newOpt1;
}
}
}
function getSelectedIndexes1(oSel3){
var selectedIndexes1 = new Array();
//alert(selectedIndexes1);
for(i=0; i < oSel3.options.length; i++){
if(oSel3.options[i].selected){
selectedIndexes1.push(i);
//alert(selectedIndexes1.push(i));
}
}
return selectedIndexes1;
}
window.onload=function(){
document.getElementById(‘btnRemoveOpts’).onclick=removeOptions;
}

// TRANSFER ALL **************************************************************************8

function transferallOptions(){
alert(“Hi”);
var tSel1 = document.getElementById(‘sel1’);
var tSel2 = document.getElementById(‘sel2’);
//var moveIndexes = moveIndexes(tSel1);
//alert(moveIndexes);
var optionAlreadyExists;
//alert(tsel1.options.length);
//transfer the selected options to sel2
for(i=0; i < tSel1.options.length; i++){
alert(tsel1.options[i].value);
optionAlreadyExists = false;
for(j=0; j < tSel2.options.length; j++){ //check if option already exists in sel2
alert(tsel2.options[j].value);
if(tSel1.options[i].value == tSel2.options[j].value){
optionAlreadyExists = true;
j = tSel2.options.length;
}
}
alert(optionAlreadyExists);
if(!optionAlreadyExists){ //add the new option
//tSel2.push(tSel1.options[i].value);

var newOptt2 = new Option(tSel1.options[i].innerHTML,tSel1.options[i].value,false,false);
alert(newOptt);
tSel2.options[tSel2.length]=newOptt2;
/*
tSel2.options[tSel2.length] = newOptt;
moveIndexes(tSel2);

}
moveIndexes(tSel2);
}
}

function moveIndexes(tSel){
var moveIndexes = new Array();

for(i=0; i < tSel.options.length; i++){
(
if(tSel.options[i].selected){

moveIndexes.push(i);
}

moveIndexes.push(itSel1.options[i].value);

}
return moveIndexes;
*/
}

window.onload=function(){
document.getElementById(‘btnTransferallOpts’).onclick=transferallOptions;
}

</script>
</head>
<body>
<button id=”btnTransferOpts” onclick=”transferOptions();”>Transfer options</button><br/>
<button id=”btnTransferallOpts” onclick=”transferallOptions();”>Transfer All</button><br/>

<button id=”btnRemoveOpts” onclick=”removeOptions();”>Remove options</button><br/>
<button id=”btnRemoveallOpts” onclick=”removeallOptions();”>Remove All</button>
<select id=”sel1″ multiple=”multiple”>
<option value=”val1″>Item 1</option>
<option value=”val2″>Item 2</option>
<option value=”val3″>Item 3</option>
<option value=”val4″>Item 4</option>
<option value=”val5″>Item 5</option>
<option value=”val6″>Item 6</option>
<option value=”val7″>Item 7</option>
</select>
<select id=”sel2″ multiple=”multiple”>
<option value=”val1″>Item 1</option>
<option value=”val7″>Item 7</option>
<option value=”val8″>Item 8</option>
<option value=”val9″>Item 9</option>
<option value=”val10″>Item 10</option>
</select>
</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 26.2012 — What do you mean by 'duplicity'?

What is it that you do not want duplicated?

?

You also realize that you have commented out a portion of one function as well as two others?
Copy linkTweet thisAlerts:
@98onamauthorMar 28.2012 — I mean to say that if suppose i have item1, item2, item3, item4, item5 in select box 1 and item1, item5, item6, item7, item8 in my select box 2...

Now in this case if i click "moveall" button only item2,3,4 should move to the select box 2...since item1 and item 5 already exist in select box 2 i dont want these items to move once again...this is wat i want 2 achieve...the action for move and remove button works perfectly without any duplicacy but i couldnt make it for moveall and remove all...
Copy linkTweet thisAlerts:
@JMRKERMar 28.2012 — Add a check to the '(re)move all' functions that look at the entry to be moved from

and compare it to the list of the entrieS to be move to.

If they match, then don't move that item, just remove it's entry from the 'from' list.
×

Success!

Help @98onam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...