Reputation: 217
I have three boxs, two of them imput boxs and one is a selector, here's a picture...
For arguments sake lets say we select 20%
How would i take what's in the Exc. VAT box and add 20% and put the resulting amount in the Inc. VAT box live, so if you change whats in Exc. VAT then Inc. VAT recalculates.
Example: Exc Rate Inc. 2.00 + 20% = 2.40
It would be nice if it could go the other way aswell, so you put an amount in Inc. VAT and it divides it by 1.20 (20%) into Exc. VAT
Upvotes: 0
Views: 743
Reputation: 3907
<form name="calculator">
<input name="exc_vat" type="text" onkeyup="calc_inc()" />
<select name="vat" onchange="calc_two()">
<option value="1.2">20%</option>
<option value="1.05">5%</option>
<option value="1">0%</option>
</select>
<input name="inc_vat" type="text" onkeyup="calc_exc()" />
</form>
<script type="text/javascript">
function calc_inc(){
var frm = document.forms.calculator;
frm.inc_vat.value = (frm.vat.value * frm.exc_vat.value).toFixed(2);
}
function calc_exc(){
var frm = document.forms.calculator;
frm.exc_vat.value = (frm.inc_vat.value / frm.vat.value).toFixed(2);
}
function calc_two(){
var frm = document.forms.calculator;
if (frm.exc_vat.value != '') calc_inc(); else if (frm.inc_vat.value != '') calc_exc();
}
</script>
Upvotes: 1
Reputation: 97575
var exc = document.getElementById(...)
var inc = document.getElementById(...)
inc.onKeyup = function() {
exc.value = inc.value / 1.2;
}
exc.onKeyup = function() {
inc.value = exc.value * 1.2;
}
Depending on how you want this to behave, you might want onInput
or onChanged
instead.
Upvotes: 0