javascript: adding values to list box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ranjini ns

    javascript: adding values to list box


    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

  • cfps.Christian

    #2
    Re: javascript: adding values to list box

    listbox1.option s[listbox1.length].text = textbox1.value;
    listbox1.option s[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.

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: javascript: adding values to list box

      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.option s[ listbox1.option s.length] = 'new element';

      to delete an element simply set it to null:

      listbox1.option s[ 1] = null;

      Comment

      • cfps.Christian

        #4
        Re: javascript: adding values to list box

        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.

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: javascript: adding values to list box

          On Jun 10, 9:35 am, "cfps.Christian " <ge0193...@otc. eduwrote:
          listbox1.option s[listbox1.length].text = textbox1.value;
          listbox1.option s[listbox1.length].value = textbox1.value;
          It's the option collection the oen with the length property, not the
          select object

          Comment

          Working...