getting combo values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anbumozhip
    New Member
    • Jul 2007
    • 10

    getting combo values

    how to get the selected combo values in a text box using on change event
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    moved from js-articles-section to forum ...

    please be a little bit more specific with your question ... you want the selected values of a multiselect select-box? ... and/or post some code you have done already ...

    kind regards

    Comment

    • anbumozhip
      New Member
      • Jul 2007
      • 10

      #3
      hi gits,
      here is my code

      [HTML]<html>
      <head>
      <script type="text/javascript">
      function moveNumbers()
      {
      var no=document.get ElementById("no ")
      var option=no.optio ns[no.selectedInde x].text
      var txt=document.ge tElementById("r esult").value
      txt=txt + option
      document.getEle mentById("resul t").value=tx t
      }
      </script>
      </head>

      <body>
      <form>
      Select numbers:<br />
      <select id="no" onchange="moveN umbers()" multiple="3">
      <option>0</option>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
      <option>9</option>
      </select>
      <input type="text" id="result" size="20">
      </form>
      </body>[/HTML]



      for the above code if suppose i select three values i need to get that values in that text box with comma separated with onchange event
      Last edited by gits; Aug 2 '07, 05:41 AM. Reason: added CODE tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        use the following function, and have a look how it works. selectedIndex only returns the first index of the selected option, so we have to loop and ask for the selected options:

        [CODE=javascript]
        function moveNumbers() {
        var no_node = document.getEle mentById("no");
        var no_node_options = no_node.childNo des;
        var result_node = document.getEle mentById("resul t");
        var result_text = '';

        for (var i = 0; i < no_node_options .length; i++) {
        var opt = no_node_options[i];

        if (opt.selected) {
        result_text += opt.text + ', ';
        }
        }

        // remove last comma through matching an regEx
        result_text = result_text.rep lace(/(, )$/, '');

        result_node.val ue = result_text;
        }
        [/CODE]
        and some additional hints ;)

        - use code-tags when posting code here in the forum
        - always use a semicolon to terminate every line of code in js

        kind regards

        Comment

        Working...