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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems dynamically changing SELECT box

Status
Not open for further replies.

phpPete

Programmer
Feb 25, 2002
88
US
The below code should add an option to a select box for each element in ana array.

I get one item added then an invalid argument error.

Any ideas?
Code:
function addToSelect(arr)
    {
    var oOption = document.createElement("OPTION");
    
    for(var i = 0; i < arr.length; i++ )        
          {
                
                oOption.text = arr[i];
                oOption.value = arr[i];
                t1.add(oOption);
          } 
        
      
    }
 
Try instantiating the option object inside your loop. What you have now will loop once, add the option, then loop again and attempt to add the same object a second time.
Code:
function addToSelect(arr)
    {
    var oOption;
    for(var i = 0; i < arr.length; i++ )        
          {
          oOption = new Option(arr[i],arr[i])
          t1.add(oOption);
          }
    }
On the fly, and my js usually has some rough edges, but this should be what your looking for. By instantiating a new option each time around you don't run into the problem of attempting to ad the same object twice.
-Tarwn ------------ My Little Dictionary ---------
Reverse Engineering - The expensive solution to not paying for proper documentation
 
Thank You Thank You Thank YOU!!!
I was at my wits end!
You just saved what little hair I have left!!

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top