I'm new in JavaScript.. Here is my question.. I have drop down list :
<div class="spanNav-row" id="spanNav-row">
<select name="timeDropList">
<option value="6">6</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="60" selected>60</option>
<option value="Vac">Vacation</option>
</select>
</div>
and I have a switch :
switch () {
case 6:
break;
case 10:
break;
case 15:
break;
case 30:
break;
case 60:
break;
default:
break;
}
How do you transfer values of drop down list to the switch?
I'm new in JavaScript.. Here is my question.. I have drop down list :
<div class="spanNav-row" id="spanNav-row">
<select name="timeDropList">
<option value="6">6</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="60" selected>60</option>
<option value="Vac">Vacation</option>
</select>
</div>
and I have a switch :
switch () {
case 6:
break;
case 10:
break;
case 15:
break;
case 30:
break;
case 60:
break;
default:
break;
}
How do you transfer values of drop down list to the switch?
Share Improve this question edited Jul 10, 2011 at 18:39 Lance Roberts 22.9k32 gold badges114 silver badges132 bronze badges asked Jul 10, 2011 at 18:25 SergioSergio 5293 gold badges12 silver badges26 bronze badges 1- Demos: jsfiddle/userdude/PCGjU You'll need to have either Chrome Console or Firefox Firebug console open to see output. – Jared Farrish Commented Jul 10, 2011 at 18:40
3 Answers
Reset to default 6Standard JS:
var ddl = document.getElementById("timeDropList");
var selectedValue = ddl.options[ddl.selectedIndex].value;
switch (selectedValue) {}
jQuery (in case you're interested):
switch($('#timeDropList option:selected').val()) {}
NOTE: To reference the drop down by getElementById(), timeDropList will also have to be the ID, not just the name:
<select id="timeDropList" name="timeDropList">
var swi_var = document.getElementById('spanNav-row').value;
swi_var
can go in the switch
If you use jQuery, it would be piece a cake:
var selectedValue = $('#spanNav-row').val();
switch (selectedValue) {
// Your switch body here.
}