0

I am designing a web page which goes like this:

<html>
<head>
<title>Bug UI</title>
</head>

<body>

<script>
    function myfunc()
    {
    //what goes here??
    }
</script>
<form>
    <select name = "parameters">
        <option value = "param1">Param 1</option>
        <option value = "param2">Param 2</option>
        <option value = "param3">Param 3</option>
        <option value = "param4">Param 4</option>
        <option value = "param5">Param 5</option>
    </select>
    <input type = "button" onclick = "myfunc()" value = "Submit">
</form>
</body>
</html>

It displays a drop-down box, when I select a value (say Param 1) from the box and click "Submit", I need to print the value (Param 1 in this case). How to achieve this?

jibs
  • 120
  • 1
  • 4
  • 15

3 Answers3

7
var s = document.getElementsByName('parameters')[0];
var text = s.options[s.selectedIndex].text;
lvil
  • 4,326
  • 9
  • 48
  • 76
1

try this work perfectly:

var ex = document.getElementsByTagName('select');
var str= ex.options[ex.selectedIndex].value;
 **or**
var str= ex.options[ex.selectedIndex].text;

or

var ex = document.getElementsByName('parameters')[0];
var str= ex.options[ex.selectedIndex].value;
      **or**
var str= ex.options[ex.selectedIndex].text;
Mr.G
  • 3,413
  • 2
  • 16
  • 20
0

Try this:

function myfunc() {
   var par=document.getElementsByName('parameters')[0];
   var index=par.selectedIndex
   console.log(par.options[index].text);
}
vusan
  • 5,221
  • 4
  • 46
  • 81