We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

jamesming on 07/23/08


Tagged

select


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

romanos


Get Selected Value


Published in: JavaScript 


  1. <form name=form0>
  2. <select name="selectFormName" onchange=getValueSelected()>
  3. <option value="Test A">Test A</option>
  4. <option value="Test B">Test B</option>
  5. <option value="Test C">Test C</option>
  6. </select>
  7. </form>
  8. <br/><br/>
  9.  
  10. <script>
  11. function getValueSelected(){
  12. var x = document.form0.selectFormName.selectedIndex;
  13. alert(document.form0.selectFormName.options[x].value);
  14. };
  15. </script

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: NeoBlack on July 23, 2008

I think, the version below is shorter and you can use it global on all select elements:

    Test A
    Test B
    Test C

function getValueSelected(obj){ alert(obj.options[obj.selectedIndex].value); };

Posted By: NeoBlack on July 23, 2008

f... comment function :D

<form name=form0> <select name="selectFormName" onchange=getValueSelected(this)> <option value="Test A">Test A</option> <option value="Test B">Test B</option> <option value="Test C">Test C</option> </select> </form> <br/><br/>

<script> function getValueSelected(obj){ alert(obj.options[obj.selectedIndex].value); }; </script>

You need to login to post a comment.