I am using the Split function to take a selected option value and split by each pipe delimeter.
The value of exploded[2] is correctly shown as 10.00 when outputted but the finance function does not return a value and my div does not get updated with fresh text.
If I swap exploded[2] to a literal 10.00 within the function like this:
finance(exploded[0], exploded[1], 10.00, exploded[3]);
it works. Similarly, if I make exploded[2] = 10.00 like this:
var test = 10.00;
finance(exploded[0], exploded[1], test, exploded[3]);
it works too. Have I done something stupid?
The Javascript
<script type="text/javascript">
$(document).ready(function() {
$('#finance').change(function() {
selected_value = $('#finance').val();
exploded = selected_value.split('|');
finance(exploded[0], exploded[1], exploded[2], exploded[3]);
});
function finance(code, order_cost, depost_pc, deposit_val) {
my_fd_obj = new FinanceDetails(code, order_cost, depost_pc, deposit_val);
$('#finance_per_month').html(my_fd_obj.m_inst);
}
});
</script>
The HTML
<select name='finance' id='finance'>
<option value="ONIF10|399.00|10.00|39.90">10 Months 0% Finance</option>
<option value="ONIF18|399.00|10.00|39.90">18 Months 0% Finance</option>
<option value="ONIF24|399.00|10.00|39.90">24 Months 0% Finance</option>
<option value="ISIB36-3.9|399.00|10.00|39.90">36 Months 3.9% Finance</option>
</select>
1 Answer 1
Impossible to say for sure without knowing more about FinanceDetails, but the difference between exploded[2] and 10.00 is that the former is a string ("10.00", not 10.00). 99% of the time, code will implicitly coerce strings to numbers, but again not knowing what's in FinanceDetails...
parseFloat(exploded[2]) might solve the problem. Or just +exploded[2].
I'd expect you would need to do this for the other args as well, if they're also meant to be numbers.
splitis working for sure, probably the issue can be found fromFinanceDetails(). Also it would be easier to pass the whole array instead of a bunch of arguments. Also you say: "... the finance function does not return a value ...", Obviously it doesn't , there's noreturn somethingin the said function, and actually there's no receiver for the return value either in the posted code.