getting drop down box value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    getting drop down box value

    Hi,

    How to get the value from drop down menu into javascript function, I need the value not the selected option.

    Here is the code
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function disp_text()
       {
       var w = document.myform.mylist.selectedIndex;
       var selected_text = document.myform.mylist.options[w].text;
       alert(selected_text);
       }
    </script>
    
    <FORM NAME="myform">
    <SELECT NAME="mylist" onChange="disp_text()">
    <OPTION VALUE="m1">Cape Fear
    <OPTION VALUE="m2">The Good, the Bad and the Ugly
    <OPTION VALUE="m3">The Omen
    <OPTION VALUE="m4">The Godfather
    <OPTION VALUE="m5">Forrest Gump
    </SELECT>
    </FORM>
    </head>
    </html>

    If the user selects an option for eg, if he selects "Cape Fear" then I will get the same thing in the javascript function, but what I need is "m1"., i.e. if he selects "The omen" I need "m3". How to do this. Please help me.

    With regards
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Try this
    Code:
    var selected_value = document.myform.mylist.options[w].value;
    Ronald

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      or even[code=javascript]var selected_value = document.myform .mylist.value;[/code]

      Comment

      • gubbachchi
        New Member
        • Jan 2008
        • 59

        #4
        Thank you for the reply

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You're welcome :)

          Comment

          Working...