how to call onselect event dropdownlist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AbhijitPat
    New Member
    • Sep 2007
    • 1

    how to call onselect event dropdownlist

    when i have select itemfrom dropdownlist i want to call javascript function
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    use the onchange-event of the <select> - element to invoke a function with the value of the control:

    [CODE=html]<script type="text/javascript">
    function show_value(val) {
    alert(val);
    }
    </script>

    <select name="test" onchange="show_ value(this.valu e);">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    </select>[/CODE]

    kind regards

    Comment

    • bond26
      New Member
      • Jan 2008
      • 1

      #3
      Hallo guys

      what if you have code like one below and want to use onselect

      Code:
              <?php
                  
                    echo "<select name='states'  onclick='getClientData(this)' >\n";
                    echo "<option value=''>==== choose title ====</option>\n";
                    $result=mysql_db_query($db_name,"select `template_id`, `template_detail`,`template_desc` from tbl_templates ");
                    while(list($id, $name,$detail)=mysql_fetch_array($result)){
                         echo "<option value=\"$id\" >$detail</option> \n" ;
                    }
                   echo "</select>\n";
                  
                  ?>
      help please
      Last edited by gits; Jul 10 '19, 04:31 AM. Reason: fix code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        please explain it in more detail ... what should happen? your current code always calls getClientData() when you click that control ... so what do you want to achieve instead or additionally?

        kind regards

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Moved to JavaScript forum.

          Comment

          • gosai jahnvi
            New Member
            • Apr 2019
            • 22

            #6
            you can use this code i hope it will help for you.

            Code:
            <select id="ddlFruits" onchange="GetSelectedTextValue(this)">
                <option value=""></option>
                <option value="1">Apple</option>
                <option value="2">Mango</option>
                <option value="3">Orange</option>
            </select>
            <script type="text/javascript">
                function GetSelectedTextValue(ddlFruits) {
                    var selectedText = ddlFruits.options[ddlFruits.selectedIndex].innerHTML;
                    var selectedValue = ddlFruits.value;
                    alert("Selected Text: " + selectedText + " Value: " + selectedValue);
                }
            </script>
            Thank you.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              if the options do have a value property this solution is definitivly inferior because of the many more operations and the solution i gave in post #2 is the one to prefer.

              Comment

              Working...