jQuery Sales Tax
- by CKallemeres
Hello everyone! I have created a function (see below) that calculates a 7.5% sales tax. Now I need help doing the following:
Have totalTax() take in 2 arguments
one for the price and one for the
tax.
On submit (use the onSubmit event
handler to call this function) have
the function process the price and
the tax by manipulating the arguments
you passed in.
Have the sales tax on the page update
dynamically with what ever the sales
tax is that you defined for the
function
7.5 percent sales tax:
Instead of using .innerHTML use
jQuery to access these document
elements and write to them:
document.getElementById('requestedAmount' ).innerHTML = priceInput;
document.getElementById('requestedTax' ).innerHTML = salesTax;
document.getElementById('requestedTotal' ).innerHTML = totalAmount;
Original Code:
<script type="text/javascript">
$().ready(function() {
// validate the comment form when it is submitted
$("#inputForm").validate();
$("#priceInput").priceFormat({
prefix: '',
limit: 5,
centsLimit: 2
});
});
function totalTax(){
var priceInput = document.getElementById( 'priceInput' ).value;
var salesTax = Math.round(((priceInput / 100) * 7.5)*100)/100;
var totalAmount = (priceInput*1) + (salesTax * 1);
document.getElementById( 'requestedAmount' ).innerHTML = priceInput;
document.getElementById( 'requestedTax' ).innerHTML = salesTax;
document.getElementById( 'requestedTotal' ).innerHTML = totalAmount;
}
</script>
<body>
<form class="cmxform" id="inputForm" method="get" action="">
<p>
<label for="priceInput">Enter the price: </label>
<input id="priceInput" name="name" class="required"/>
</p>
<p>
<input class="submit" type="submit" value="Submit" onclick="totalTax();"/>
</p>
</form>
<div>Entered price:
<p id="requestedAmount"></p>
</div>
<div>7.5 percent sales tax:
<p id="requestedTax"></p>
</div>
<div>Total:
<p id="requestedTotal"> </p>
</div>