[Javascript] How can i create new <OPTION> elements of a visible <SELECT> el

Bill Marriott bill.marriott at optusnet.com.au
Wed Nov 21 13:51:23 CST 2001


Here is some code that may give you some ideas.
I use it to dynamically change the options in one select depending on the
choice (filter) of the other select.
the options for the select that changes are all stored in an array. ( 3 part
array , value, text and filter)
I call the function from the onchange event of the first select.

var filter = 0 ;
function setSelOptions()
{

 var sel1Obj = document.getElementById("firstselect");
 filter=sel1Obj.value;
 var sel2Obj = document.getElementById("secondselect");


 sel2Obj.options.length = 0;          //// clear out all the existing
options
 var i;
  for (i = 0; i < selArray.length; i++)
    {
        if (filter == selArray[i][2])


           var pOption=document.createElement("OPTION");
           pOption.value=selArray[i][0];
           pOption.text=selArray[i][1];
           sel2Obj.options.add(pOption);
          }
    }
}

good luck with the invisible part

Bill




More information about the Javascript mailing list