INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

dynamic listboxes !

how to dynamically modify the list in a listbox ? by iza
Posted: 14 Dec 00


/* IE4+ ONLY !!!!!!!
it is suposed that :
   the form name is my_form;
   the listbox which we want to change the content is my_listbox;
   the datas we want to display in it are stored in an my_array (yes, everything is mine ! );
   my_array is 2dimensionnal : my_array[x][0] is the value, my_array[x][1] is the text corresponding to the value
*/

my_listbox=document.my_form.my_listbox

//------------
//POPULATING MY_LISTBOX WITH MY_ARRAY VALUES
// in : nothing; out : true (always !)

function fill_my_listbox()
{
   // first, remove all what is in the listbox
   while (my_listbox.options.length!=0)
      my_listbox.options.remove(0);
   
   // now, fill it !
   for (var m=0; m<my_array.length; m++)
      // if you conditionnaly fill the list, put the condition here (for exemple : if (my_array[m][1]>"whatever"))
   {
        var oOption = document.createElement("OPTION");
    oOption.text=my_array[m][1];
       oOption.value=my_array[m][0];
       my_listbox.options.add(oOption)
   }
   return true;

}

//---------------
//ADDING JUST ONE OPTION TO MY_LISTBOX
// in : text is the text of the added option, value its value; out : true (always !)

function add_one_option (text, value) {
 var oOption = document.createElement("OPTION");
 oOption.text=text;
 oOption.value=value;
 my_listbox.options.add(oOption)
 return true;
}

//--------------
//REMOVING JUST ONE OPTION FROM MY_LISTBOX
// in : my_value the value of the option you want to remove; out : boolean

function rem_one_option(my_value) {
  var my_index=-1
  for (var m=0; m<my_listbox.options.length; m++) // first we have to retrieve the index ! if someone knows a better function ... i'm sure there is ...
       if (my_listbox.options[m].value==my_value)
           my_index=m;
  // so now the index should be in my_index
  if (my_index==-1) // someting wrong happened
  {
     alert("well i'm not able to find the index of the element corresponding to the value "+my_value)
     return false;
   }
   else     
      my_listbox.options.remove(my_index)
}

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close