javascript in listbox
-
function fnMoveItems(lstbxFrom,lstbxTo) { var varFromBox = document.all(lstbxFrom); var varToBox = document.all(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) { alert('Please select an Item to move'); return false; } while ( varFromBox.options.selectedIndex >= 0 ) { var newOption = new Option(); newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; varToBox.options[varToBox.length] = newOption; varFromBox.remove(varFromBox.options.selectedIndex); } } return false; } The above is my javascript code to move the values from one listbox to another. Once it move the values , the selected values are deleted from the sourceListbox(ie., left side listbox). Actually my task is , it never deletes the selected value . pls give the alternate code for it.
-
function fnMoveItems(lstbxFrom,lstbxTo) { var varFromBox = document.all(lstbxFrom); var varToBox = document.all(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) { alert('Please select an Item to move'); return false; } while ( varFromBox.options.selectedIndex >= 0 ) { var newOption = new Option(); newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; varToBox.options[varToBox.length] = newOption; varFromBox.remove(varFromBox.options.selectedIndex); } } return false; } The above is my javascript code to move the values from one listbox to another. Once it move the values , the selected values are deleted from the sourceListbox(ie., left side listbox). Actually my task is , it never deletes the selected value . pls give the alternate code for it.
I have modified your code, you can use it. function fnMoveItems(lstbxFrom,lstbxTo) { //var varFromBox = document.all(lstbxFrom); //var varToBox = document.all(lstbxTo); //You can access by list box id like //var varFromBox = document.getElementById('lstbxFromID'); //var varToBox = document.getElementById('lstbxToID'); 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) { alert('Please select an Item to move'); return false; } var i; for(i=varFromBox.options.length-1;i>=0;i--) { if(varFromBox.options[i].selected) { var optn = document.createElement("OPTION"); optn.text = varFromBox.options[i].text; optn.value = varFromBox.options[i].value; lstbxTo.options.add(optn); varFromBox.remove(i); } } } return false; }
Parwej Ahamad g.parwez@gmail.com