I need to insert an Amount in a text field ... when I leave that field, I must calculate the tax on that amount (tax = amount * 0.12) also the total ( total = amount + tax) and set these values in other fields of text automatically.
HTML code
<input type="text" id="Monto" name="Monto" placeholder="Monto" onchange="CalculoIva()" required />
<input type="text" id="Iva" name="Iva" placeholder="Iva" required />
<input type="text" id="Total" name="Total" placeholder="Total" required />
I'm trying this but it doesn't work
<script lang="javascript" type="text/javascript">
var monto = 0;
onchange = CalculoIva();
function CalculoIva() {
monto = document.getElementById('Monto').innerText ;
var iva = parseFloat(monto) * 0.12;
var total = parseFloat(monto) + parseFloat(iva);
document.getElementById("Iva").innerHTML = iva.toString();
document.getElementById("Total").innerHTML = total.toString();
}
</script>
you should do something like this
I have refactored the code from sioesi 's answer a bit
iva
andtotal
changed toreadonly
CalculoIva()
is now reusablemonto
is numericiva
andtotal
to 2 decimals (0.00)