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

how to dynamically modify the list in a listbox ?

dynamic listboxes !

how to dynamically modify the list in a listbox ?

by  iza  Posted    (Edited  )

/* 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)
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top