I want to sum some values onChange
HTML select box. My problem is that when I change selected value the value on "display" does not change.
var Price = document.getElementById("price"),
PriceSValue = Price.value;
var sum = 0;
var a = PriceSValue;
var b = '1000';
sum = a + b;
document.getElementById('display').innerHTML = sum;
<select id="price" class="form-control" name="two">
<option value="100">Monthly</option>
<option value="200">3 month</option>
<option value="300">6 month</option>
<option value="400">6 month</option>
</select>
<div id="display"></div>
2
Answers
If you want to add an event listener to the
select#price
element, you need to either add it via JavaScript (preferred) or inline with the HTML markup.Also, you should use integer values instead of strings. If you add a number to a string, it is treated as normal string concatenation.
Note: Not sure why you have "6 month" twice. Are these computations supposed to be multiplicative rather than additive?
JavaScript Only
HTML + JavaScript
You need to add an
onChange=""
event, so it will call a function which will sum your values. You can pass the selection value by adding an argument to the function usingthis.value
. Furthermore, you need to convert every number into anint
, so it will sum up.