I have already created this using jQuery. You can view it on JSFiddle: JSFiddle
HTML:
<div class="container">
<header>
<h3>The Crazy Things We'll Do for Money</h3>
<div class="small"><em>An elegant way to test Knockout JS that you're considering.</em>
</div>
</header>
<ul>
<li>
<input type="checkbox" name="services" value="150" id="srv" />
<span>Throw knives at us while we're strapped on a turntable ($150)</span>
</li>
<li>
<input type="checkbox" name="services" value="200" id="srv" />
<span>Watch us get chased by bulls ($200)</span>
</li>
<li>
<input type="checkbox" name="services" value="799.50" id="srv" />
<span>Watch us in a straight jacket immersed in a pool of electric eels and try to escape Houdini style. ($799.50)</span>
</li>
</ul>
<hr/>
<div class="marginise">
<strong>I want to help with your hospital bills</strong>
<select id="donate" name="donate">
<option value="0">No Thanks</option>
<option value="100">$100</option>
<option value="300">$300</option>
<option value="500">$500</option>
</select>
<p><strong>Donations</strong>: <span id="blah"></span>
</p>
</div>
<div class="est">
<h4>Total</h4>
<span id="serviceTotal"></span>
</div>
<div class="f">These Events / Entertainment Services are not real. Unfortunately.</div>
</div>
jQuery:
function outputValue() {
mathUsage();
var donValues = $("#donate").val();
$("#blah").html(numeral(donValues).format('$0,0[.]00'));
}
var $cap = $('input[name="services'];
function mathUsage() {
var total = $("#donate").val();
$cap.each(function () {
if (this.checked) total = parseFloat(total) + parseFloat(this.value);
});
$("#serviceTotal").text(numeral(total).format('$0,0[.]00'));
}
$("select").change(outputValue);
outputValue();
$cap.click(mathUsage);
I am looking to recreate this using knockoutJS so I can reduce the amount of HTML markup. Any suggestions on the best approach?
There are similar examples out there like this one on Fiddle that show how to output checkbox values in an array. I was thinking of utilizing a similar method to calculate a sum but unsure of where to begin or which syntax to use.