Forum Moderators: open

Message Too Old, No Replies

JS move items from a listbox

         

andrewsmd

2:46 pm on May 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have two listboxes that one is populated with data and the other one is to store the items they select. I have the single move button working, but I cannot get the move all button. What is the problem here is my JS codefunction test(bxFrom, bxTo) {

var varFromBox = document.getElementById(bxFrom);
var varToBox = document.getElementById(bxTo);

if (varFromBox.length < 1) {
alert('There are no items in that box');
return false;
} //if

for (var i = 0; i <= varFromBox.options.length; i++) {
msg = msg + i + "\n";
var newOption = new Option(); // Create a new instance of ListItem

newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox

varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox

} //for

}//test

something is wrong with that function. What is it? Thanks,

Fotiman

5:38 pm on May 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Several problems.

if (varFromBox.length < 1) {
should be
if (varFromBox.options.length < 1) {

Also:
for (var i = 0; i <= varFromBox.options.length; i++) {
should be
for (var i = 0; i < varFromBox.options.length; i++) {

And then inside that for loop:


if (varFromBox.options[i].selected) {
msg = msg + i + "\n";
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = varFromBox.options[i].text;
newOption.value = varFromBox.options[i].value;
varToBox.options[varToBox.options.length] = newOption; //Append the item in Target Listbox
varFromBox.remove(i); //Remove the item from Source Listbox
}

Fotiman

5:39 pm on May 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, I didn't test the code above. :)