Posted By

jamesming on 07/23/08



Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


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/>
  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  


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.