I have a page on the site that calculates the final rental rates, which are optional. Javascript serves nothing but to show you the updated amount if you select or deselect the checkboxes.
Here is what I have tried. It seems to work, but it adds 2 cents to every other click for one of the flags, I think, because for the rounds. How can I keep the number from rounding?
function update_rate(state, amount, field) { if (state == true) { rate = parseFloat($('span.rateTotal').text()) + amount; rate = rate.toFixed(2); due = parseFloat($('span.'+field).text()) + amount; due = due.toFixed(2); $('span.rateTotal').text(rate); $('span.'+field).text(due); } else { rate = parseFloat($('span.rateTotal').text()) - amount; rate = rate.toFixed(2); due = parseFloat($('span.'+field).text()) - amount; due = due.toFixed(2); $('span.rateTotal').text(rate); $('span.'+field).text(due); } }
HTML checkbox:
<cfinput type="checkbox" name="linen_service" id="linen_service" checked="checked" value="0" onClick="update_rate(this.checked, #reservationQuery[7].xmlChildren[i]['dblAmount'].xmlText#, 'second-due');" />
Basically conveys the number of options, the state of the flag, and the name of the field that should be affected.
Edit: Fiddle URL: http://jsfiddle.net/nGrVf/6/
source share