By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,714 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,714 IT Pros & Developers. It's quick & easy.

javascript: adding values to list box

P: n/a

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

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jun 27 '08 #2

P: n/a
On Jun 10, 8:33*am, ranjini ns wrote:
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;
Jun 27 '08 #3

P: n/a
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.
Jun 27 '08 #4

P: n/a
On Jun 10, 9:35*am, "cfps.Christian" <ge0193...@otc.eduwrote:
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.