Created
November 14, 2023 03:12
-
-
Save wpgaurav/455a77698c50322f2a4b47eb6d649540 to your computer and use it in GitHub Desktop.
Open source code for Sales Tax Calculator on https://gauravtiwari.org/apps/sales-tax-calculator/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="salesTaxCalculator" class="block-half has-border"> | |
<div class="form-group my-3"> | |
<label class="form-check form-check-inline"> | |
<input class="form-check-input" type="radio" name="taxType" value="exclusive"> | |
<span class="form-check-label">Tax Exclusive</span> | |
</label> | |
<label class="form-check form-check-inline"> | |
<input class="form-check-input" type="radio" name="taxType" value="inclusive"> | |
<span class="form-check-label">Tax Inclusive</span> | |
</label> | |
</div> | |
<div class="form-group mb-3"> | |
<label class="form-label">Amount</label> | |
<input type="number" id="amount" class="form-control" required> | |
</div> | |
<div class="form-group mb-3"> | |
<label class="form-label">Sales Tax Rate</label> | |
<div class="input-group box-lr"> | |
<input type="number" step="any" id="rate" class="form-control" required> | |
<span class="input-group-text">%</span> | |
</div> | |
</div> | |
<div class="form-group mb-3 text-center"> | |
<button type="button" class="btn btn-info" onclick="calculateTax()">Calculate</button> | |
<button type="button" class="btn btn-lime" onclick="sampleInput()">Sample</button> | |
<button type="button" class="btn btn-warning" onclick="resetForm()">Reset</button> | |
</div> | |
<div class="form-group box-lr"> | |
<label class="form-label">Calculated Tax:</label> | |
<div id="calculatedTax"></div> | |
</div> | |
</form> | |
<script> | |
function calculateTax() { | |
var amount = parseFloat(document.getElementById('amount').value); | |
var rate = parseFloat(document.getElementById('rate').value) / 100; | |
var taxType = document.querySelector('input[name="taxType"]:checked').value; | |
var calculatedTax; | |
if (taxType === 'exclusive') { | |
calculatedTax = amount * rate; | |
} else { | |
calculatedTax = amount - (amount / (1 + rate)); | |
} | |
document.getElementById('calculatedTax').innerText = '$' + calculatedTax.toFixed(2); | |
} | |
function sampleInput() { | |
document.getElementById('amount').value = '100'; | |
document.getElementById('rate').value = '8'; | |
document.querySelector('input[name="taxType"][value="exclusive"]').checked = true; | |
} | |
function resetForm() { | |
document.getElementById('salesTaxCalculator').reset(); | |
document.getElementById('calculatedTax').innerText = ''; | |
} | |
</script> | |
<style> | |
/* Add your CSS styling here */ | |
.form-group { | |
margin-bottom: 15px; | |
} | |
.input-group-text { | |
background-color: #e9ecef; | |
border: 1px solid #ced4da; | |
border-radius: 0.25rem; | |
padding: 0.375rem 0.75rem; | |
} | |
.form-control { | |
display: block; | |
width: 100%; | |
padding: 0.375rem 0.75rem; | |
font-size: 1rem; | |
line-height: 1.5; | |
color: #495057; | |
background-color: #fff; | |
background-clip: padding-box; | |
border: 1px solid #ced4da; | |
border-radius: 0.25rem; | |
} | |
.btn { | |
display: inline-block; | |
font-weight: 400; | |
text-align: center; | |
vertical-align: middle; | |
user-select: none; | |
border: 1px solid transparent; | |
padding: 0.375rem 0.75rem; | |
font-size: 1rem; | |
line-height: 1.5; | |
border-radius: 0.25rem; | |
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; | |
} | |
.btn-info { | |
color: #fff; | |
background-color: #17a2b8; | |
border-color: #17a2b8; | |
} | |
.btn-lime { | |
color: #fff; | |
background-color: #22a340; | |
border-color: #22a340; | |
} | |
.btn-warning { | |
color: #212529; | |
background-color: #ffc107; | |
border-color: #ffc107; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment