javascript: adding values to list box

  • Thread starter Thread starter ranjini ns
  • Start date Start date
R

ranjini ns

Hi,

I have a form, and in this form, there are 2 drop down list a text box, one button (labelled "add" )and finally, a listbox.

The question is, when the user clicks on the add button, how do I add the value selected in the drop down list and the text box values and put it into the listbox? After adding to the listbox, the selected value needs to stay in the drop down menu and not get removed. Thanks in advance
 
listbox1.options[listbox1.length].text = textbox1.value;
listbox1.options[listbox1.length].value = textbox1.value;

listbox1 would also contain a .selected value for the selected line as
well as .selectedIndex, however it should keep your selected index.
 
Hi,

I have a form, and in this form, there are 2 drop down list a text box, one button (labelled "add" )and finally, a listbox.

The question is, when the user clicks on the add button, how do I add the value selected in the drop down list and the text box values and put it intothe listbox? After adding to the listbox, the selected value needs to stay in the drop down menu and not get removed. Thanks in advance

Hi,

That is a JS question, not a C# one,
In any case all y ou have to do is adding the new element to the list:
listbox1.options[ listbox1.options.length] = 'new element';

to delete an element simply set it to null:

listbox1.options[ 1] = null;
 
That is a JS question, not a C# one,

I think I might have interpreted it as a Javascript question.

If you're doing it in C# just combine the drop down item and the
textbox however you were going to in the Add method and if the listbox
isn't keeping the selected index just make a note of the selected
index add the item then set the selected index back to what it should
be. Where you might run into problems is if your listbox gets too
long and the scrollbars start moving on you, in that case you can
search for a scrolling listbox on www.codeproject.com.
 
listbox1.options[listbox1.length].text = textbox1.value;
listbox1.options[listbox1.length].value = textbox1.value;

It's the option collection the oen with the length property, not the
select object
 
Back
Top