Is the following code a proper solution to my problem? I have a feeling it can be simplified quite a bit.
If one of the first four checkboxes is checked the sum is 1; if two 1.5; if three 2; if four 2.5. If the bar5 is checked then it's 1 + calculated value of first four (if any is checked). Afterwards, multiply by 100.
function checkboxCount() {
var form = document.forms["calculator"];
var bar5 = form.elements["bar5"];
var foo = document.querySelectorAll("input[name=foo]");
count = 0;
for (var i = 0; i < foo.length; i++) {
if (foo[i] && foo[i].checked == true) {
count++;
}
}
if (count == 1 && bar5.checked == true) {
count = 1;
} else if (count == 1) {
count = 1;
}
if (count == 2 && bar5.checked == true) {
count = 2;
} else if (count == 2) {
count = 1.5;
}
if (count == 3 && bar5.checked == true) {
count = 2.5;
} else if (count == 3) {
count = 2;
}
if (count == 4 && bar5.checked == true) {
count = 3;
} else if (count == 4) {
count = 2.5;
}
if (count == 5 && bar5.checked == true) {
count = 3.5;
}
return count;
}
function calculateTotalCost() {
var totalCost = 0;
var printCost = document.getElementById('calculatedCost');
totalCost = 100 * checkboxCount();
printCost.innerHTML = "$" + totalCost;
}
<form id="calculator">
<fieldset>
<label><input type="checkbox" name="foo" id="bar1" onclick="calculateTotalCost()">Bar 1</label>
<label><input type="checkbox" name="foo" id="bar2" onclick="calculateTotalCost()">Bar 2</label>
<label><input type="checkbox" name="foo" id="bar3" onclick="calculateTotalCost()">Bar 3</label>
<label><input type="checkbox" name="foo" id="bar4" onclick="calculateTotalCost()">Bar 4</label>
<label><input type="checkbox" name="foo" id="bar5" onclick="calculateTotalCost()">Bar 5</label>
</fieldset>
<span id="calculatedCost"></span>
</form>