I have made quite a bunch of calculators, that is why I gave it a little bit more work.
Look at my solution in jsFiddle.
<style type='text/css'>
table td,
table th {
border: 1px solid #DDD;
padding: 2px 5px;
}
.totalnetworth .value {
font-weight: bold;
}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function () {
$('.btcowned').keyup(updateNumbers);
$('.feebox').keyup(updateNumbers);
});
function updateNumbers(e) {
// Preparation
var $calc = $(this).closest('.calculator'),
$results = $calc.find('table'),
chosen = parseFloat($calc.find('.btcowned').val()),
fee = parseFloat($calc.find('.feebox').val()),
vatPercent = 25,
// Calculation
vatRate = vatPercent * 0.01 + 1,
val = chosen - ((chosen * fee) / 100),
vatVal = val * vatRate;
// Representation
$results.find('.totalnetworth .novat').html(isNaN(val) ? '-' : Number(val).toFixed(2));
$results.find('.totalnetworth .wvat').html(isNaN(vatVal) ? '-' : Number(vatVal).toFixed(2));
}
});//]]>
</script>
<div class="calculator">
<label for="btcowned-1">btcowned
<input id="btcowned-1" class="btcowned" value="" />
</label>
<label for="btcowned-1">Fee
<input id="feebox-1" class="feebox" value="4.5" />
</label>
<table>
<tr>
<th>Title</th>
<th>Cost</th>
<th>Cost+VAT</th>
</tr>
<tr class="totalnetworth">
<td>Transfer</td>
<td class="novat value"></td>
<td class="wvat value"></td>
</tr>
</table>
</div>
And some comments regarding your code:
- Don't use ID's for your scripts;
- Close only void tags like
area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr
;
- Don't trust user input to give correct data format, as it can crash your scripts with ease;
- Always try to separate preparation and calculation part from representation part in your scripts, that will make it cleaner and it will be much easier to catch bugs.