Click to See Complete Forum and Search --> : Working with a list box dynamically


Dragons_Bane
05-18-2003, 12:40 PM
Hey all. I want to take Options from one listbox and put them in another one when someone clicks the "Add" button and I'm getting a strange error when doing this.

Here's my code

function btnAdd_onclick()
{

var flist = document.forms("Form1").List1;
var olist = document.forms("Form1").List2;
var item = new Option;

item = flist.options(flist.selectedIndex);
olist.options.add(item);
return 0;

}


And from what I found on another site ths should work but I'm getting an 'Object Expected' Error on line 10 (the '{' that starts the function) I think the rest will work but I don't know what's going on.

Additional Info... List1 Has <OPTIONS> in it, but List2 doesn't, would that make a difference (I don't think it would)

HELP!!! this is frustrating when I can't find the error.

And Thanks in advance.
Shawn

AdamGundry
05-18-2003, 12:56 PM
document.forms and flist.options are arrays, not functions. Use square brackets instead.

Adam

Dragons_Bane
05-18-2003, 12:59 PM
Thanx, I haven't worked with Java (or JavaScript) in a while, and now everything is in square brackets but it's not loading anything into the other list box (but the error message is gone :))

AdamGundry
05-18-2003, 01:24 PM
This should work. If you don't want to lose the option from the first listbox, comment the last line.

function btnAdd_onclick(){
var flist = document.forms["Form1"].List1;
var olist = document.forms["Form1"].List2;
var item = new Option(flist.options[flist.selectedIndex].text, flist.options[flist.selectedIndex].value);
olist.options[olist.length] = item;
flist.options[flist.selectedIndex] = null;
}

Adam

Dragons_Bane
05-18-2003, 01:27 PM
Thanx Adam, It works Now :)