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

how to call onselect event dropdownlist

P: 1
when i have select itemfrom dropdownlist i want to call javascript function
Sep 27 '07 #1
Share this Question
Share on Google+
6 Replies


gits
Expert Mod 5K+
P: 5,282
hi ...

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

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function show_value(val) {
  3.     alert(val);
  4. }
  5. </script>
  6.  
  7. <select name="test" onchange="show_value(this.value);">
  8.     <option value="a">a</option>
  9.     <option value="b">b</option>
  10.     <option value="c">c</option>
  11. </select>
kind regards
Sep 28 '07 #2

P: 1
Hallo guys

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

Expand|Select|Wrap|Line Numbers
  1.         <?php
  2.  
  3.               echo "<select name='states'  onclick='getClientData(this)' >\n";
  4.               echo "<option value=''>==== choose title ====</option>\n";
  5.               $result=mysql_db_query($db_name,"select `template_id`, `template_detail`,`template_desc` from tbl_templates ");
  6.               while(list($id, $name,$detail)=mysql_fetch_array($result)){
  7.                    echo "<option value=\"$id\" >$detail</option> \n" ;
  8.               }
  9.              echo "</select>\n";
  10.  
  11.             ?>
  12.  
help please
Feb 1 '08 #3

gits
Expert Mod 5K+
P: 5,282
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
Feb 1 '08 #4

acoder
Expert Mod 15k+
P: 16,027
Moved to JavaScript forum.
Feb 4 '08 #5

P: 19
you can use this code i hope it will help for you.

Expand|Select|Wrap|Line Numbers
  1. <select id="ddlFruits" onchange="GetSelectedTextValue(this)">
  2.     <option value=""></option>
  3.     <option value="1">Apple</option>
  4.     <option value="2">Mango</option>
  5.     <option value="3">Orange</option>
  6. </select>
  7. <script type="text/javascript">
  8.     function GetSelectedTextValue(ddlFruits) {
  9.         var selectedText = ddlFruits.options[ddlFruits.selectedIndex].innerHTML;
  10.         var selectedValue = ddlFruits.value;
  11.         alert("Selected Text: " + selectedText + " Value: " + selectedValue);
  12.     }
  13. </script>
Thank you.
1 Week Ago #6

gits
Expert Mod 5K+
P: 5,282
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.
1 Week Ago #7

Post your reply

Sign in to post your reply or Sign up for a free account.