I am changing a variable based on the value selected from a dropdown. I have something similar working with an if/else statement but when I try it with switch I can not get it to work. I made a fiddle of this here
html :
<select class="heiSelect">
<option value='3924'>6'1"</option>
<option value='3923'>6'2"</option>
<option value='3922'>6'3"</option>
<option value='3921'>6'4"</option>
<option value='3920'>6'5"</option>
<option value='3919'>6'6"</option>
<option value='3918'>6'7"</option>
<option value='3917'>6'8"</option>
<option value='3916'>6'9"</option>
<option value='3915'>6'10"</option>
</select>
<h1>Results : </h1>
javascript:
$(function() {
var hvd = 0;
$('.heiSelect').change(function() {
var heiValue = $('.heiSelect').val();
switch (heiValue) {
case 3915:
hvd = 4294964526;
break;
case 3916:
hvd = 4294964528;
break;
case 3917:
hvd = 4294964529;
break;
case 3918:
hvd = 4294964406;
break;
case 3919:
hvd = 4294964495;
break;
case 3920:
hvd = 4294964494;
break;
case 3921:
hvd = 4294964493;
break;
case 3922:
hvd = 4294964492;
break;
case 3923:
hvd = 4294964491;
break;
case 3924:
hvd = 4294964490;
break;
}
$('h1').append(heiValue + " + " + hvd + ", ");
});
});
Can you see the problem?
The value is a string, while you tested them against numbers.
To fix it, either parse the string into integer:
or use strings in the cases: