I am trying to get the dropdowns in the code below working like the checkboxes. The checkboxes when checked reveal a price and the revealed prices then get totaled. I also want the option selected in the dropdowns to reveal a price and be added to the total. My issue is with the coding in $('select').change(function() {
Appreciate any help.
Thanks
See: https://jsfiddle.net/hcanning2012/po4189af/130/
HTML
<table width="100%">
<tr>
<td>
<label>
<input type="checkbox" name="colorCheckbox" value="n1"> Show Price
</label>
</td>
<td>
<div class="n1 box">200</div>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" name="colorCheckbox" value="n2"> Show Price
</label>
</td>
<td>
<div class="n2 box">200</div>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" name="colorCheckbox" value="n3"> Show Price
</label>
</td>
<td>
<div class="n3 box">200</div>
</td>
</tr>
<tr>
<td>
<label>
<select value="n4" id="n4">
<option value="0" selected>Choose...</option>
<option value="10">item 1</option>
<option value="20">item 2</option>
</select>
</td>
<td>
<div class="n4"></div>
</td>
</tr>
<tr>
<td>
<label>
<select value="n5" id="n5">
<option value="0" selected>Choose...</option>
<option value="3">item 1</option>
<option value="4">item 2</option>
</select>
</td>
<td>
<div class="n5"></div>
</td>
</tr>
<tr>
<td > Total: </td><td><div id="sum">0</div></td>
</tr>
CSS
.box {
display: none;
}
jQuery
$(document).ready(function() {
var sum = 0;
$('input[type="checkbox"]').change(function() {
var inputValue = $(this).attr("value");
if(this.checked) {
sum = parseInt(sum) + parseInt($("." + inputValue).text());
}else {
sum = parseInt(sum) - parseInt($("." + inputValue).text());
}
$("#sum").text(sum);
$("." + inputValue).toggle();
});
$('select').change(function() {
var selectValue = $(this).attr("value");
var optionValue = (this.value);
$("." + selectValue).text(optionValue);
if('option:selected',this) {
sum = parseInt(sum) + parseInt($("." + selectValue).text());
}else {
sum = parseInt($("." + selectValue).text());
}
$("#sum").text(sum);
});
});
Thanks