Using JScript in DHTML to Manipulate Objects in a SELECT tag

The following code can be used to dynamically add, remove or select all items in a SELECT object.  The code assumes that when objects are added to the SELECT object, that the display text and value are identical. 

<script language="javascript"> 
   function addToList(txtBox, selectBox){ 
      var oOption = document.createElement("OPTION"); 
      oOption.text = txtBox.value; 
      oOption.value = txtBox.value; 
      txtBox.value = ""; 

   function removeFromList(selectBox){ 

   function selectEntireList(selectBox){ 
      var fromObj = selectBox; 
      for ( selIndex = fromObj.length; selIndex -- ; selIndex > 0 ){ 
         if(fromObj.options[selIndex].text != ""){ 
            fromObj.options[selIndex].selected = true; 

Comments (1)
  1. Bjoern Graf says:

    After spending 10 minutes on analysing the for loop in selectEntireList I have to admit it is pure zen: iterating throught the options bottom-to-top without a var si = sel.options.length – 1 as initial index by swapping the for’s increment (decrement here) and test are just masterly done. Placing the selIndex > 0 test in the increments place for documentional reasons is ingenious 😀

    I really like this loop.


    PS: to add something more serious to this comment: a few tests on dynamically adding options to selects can be found at

Comments are closed.

Skip to main content