1<select id="ddlViewBy">
2 <option value="1">test1</option>
3 <option value="2" selected="selected">test2</option>
4 <option value="3">test3</option>
5</select>
6
7<script>
8 var e = document.getElementById("ddlViewBy");
9 var strUser = e.value; // 2
10 var strUser = e.options[e.selectedIndex].text; //test2
11</script>
1(function() {
2
3 // get references to select list and display text box
4 var sel = document.getElementById('scripts');
5 var el = document.getElementById('display');
6
7
8 function getSelectedOption(sel) {
9 var opt;
10 for ( var i = 0, len = sel.options.length; i < len; i++ ) {
11 opt = sel.options[i];
12 if ( opt.selected === true ) {
13 break;
14 }
15 }
16 return opt;
17 }
18
19 // assign onclick handlers to the buttons
20 document.getElementById('showVal').onclick = function () {
21 el.value = sel.value;
22 }
23
24 document.getElementById('showTxt').onclick = function () {
25 // access text property of selected option
26 el.value = sel.options[sel.selectedIndex].text;
27 }
28
29 document.getElementById('doLoop').onclick = function () {
30 var opt = getSelectedOption(sel);
31 el.value = opt.value;
32 }
33
34}());
35// immediate function to preserve global namespace
36