I understand this may be a repeat question but I have been searching for ages and cant figure out why this isnt working.
I have 3 input fields, Subtotal, Vat and Total: I want to be able to populate the VAT and Total inpur fields with values when there is a value inputted in Subtotal and to show 2 decimal palces after. So:
4 would be 4.00
4.5 would be 4.50
HTML code for the input field:
<input name="subtotal" id="subtotal" type="number" maxlength="20" min="0" placeholder="00.00" onchange="vatCalculation();" />
<input name="vat" id="vat" type="number" maxlength="20" min="0" placeholder="00.00" readonly="true" />
<input name="total" id="total" type="number" maxlength="20" min="0" placeholder="00.00" readonly="true" />
And the javascript code I have at the moment is:
function vatCalculation() {
var subtotal = document.getElementById('subtotal').value;
var vat = parseFloat(parseFloat(subtotal) * parseFloat(0.2)).toFixed(2);
var total = parseFloat(parseFloat(subtotal) + parseFloat(vat)).toFixed(2);
document.getElementById('vat').value = vat;
document.getElementById('total').value = total;
}
I cant see where I am going wrong. When I enter 10 in the 'subtotal'input field the 'VAT' and 'Total' fields change to 2 and 12. But I want them to show 2.00 and 12.00. Screenshot below:
SOLUTION:
When using Firefox the input field of type="number" dont seem to work with javascript calculation. Workaround is to change it to a type="text" Like J Santosh as mentioned below and it works.
Found the issue . It is with <input type='number'> you change it to <input type='text'>
Working Fiddle
Input Type Number is not accepting decimals
Reference -1
Reference-2
Is this what you want? If so, you were pretty close. You just needed to add
document.getElementById('subtotal').value = parseFloat(subtotal).toFixed(2);
to your code as well.
function vatCalculation() {
var subtotal = document.getElementById('subtotal').value;
var vat = parseFloat(parseFloat(subtotal) * parseFloat(0.2)).toFixed(2);
var total = parseFloat(parseFloat(subtotal) + parseFloat(vat)).toFixed(2);
document.getElementById('subtotal').value = parseFloat(subtotal).toFixed(2);
document.getElementById('vat').value = vat;
document.getElementById('total').value = total;
}
<input name="subtotal" id="subtotal" type="text" maxlength="20" min="0" placeholder="00.00" onchange="vatCalculation();" />
<input name="vat" id="vat" type="text" maxlength="20" min="0" placeholder="00.00" readonly="true" />
<input name="total" id="total" type="text" maxlength="20" min="0" placeholder="00.00" readonly="true" />
(2.399).toFixed(2); will give you 2.40
But if you need 2.39 try
parseInt(2.399 * 100)/100
Related
As shown in the image below, I have the Sell Price textbox and Use Sell Price Percentage textbox. My Use Sell Price Percentage have this code
<div class="checkbox">
<label>
<input type="checkbox" name="sellpCheck" id="scheck" onclick="document.getElementById('sell_price').disabled=this.checked;">
* Use Sell Price Percentage
</label>
and here's my Sell Price textbox
<input type="number" class="form-control" name="sell_price" id="sell_price" step='0.01' value='0.00' placeholder='0.00' required>
lastly, my Buy Price textbox
<input type="number" class="form-control" name="buy_price" placeholder="Buying Price Per Unit"
step='0.01' value='0.00' placeholder='0.00' min="0" max="9999" required id="buyp">
Here's my initial jQuery code:
var buyp = parseInt($('#buyp').val());
var sellp = parseFloat($('#sellp').val());
$('#sell_price').val((buyp * ((sellp/100)+1)).toFixed(2))
What I want to happen, is when I click the Use Sell Price Percentage the Sell Price textbox will automatically compute and can also recompute if I change the buyprice onkeyup.
You would have to do something like this then,
In HTML:
<input type="checkbox" name="sellpCheck" id="scheck" onclick="document.getElementById('sell_price').disabled=this.checked;checkThis();">
In JS:
function checkThis(){
var buyp = parseInt($('#buyp').val());
var sellp = parseFloat($('#sellp').val());
$('#sell_price').val((buyp * ((sellp/100)+1)).toFixed(2));
}
For the change in buyPrice you could use onblur:
In HTML:
<input type="number" class="form-control" name="buy_price" placeholder="Buying Price Per Unit"
step='0.01' value='0.00' placeholder='0.00' min="0" max="9999" required id="buyp" onblur="checkThis()">
I'd like to force an <input type="number" step="0.01" /> to always have 2 decimals to enter accounting data.
I've managed to do that using JavaScript
document.getElementById('input').addEventListener('change', force2decimals);
function force2decimals(event) {
event.target.value = Number(Math.round(event.target.value * 100) / 100).toFixed(2);
}
<input type="number" step="0.01" id="input" value="1.00" />
Is there any way to handle this natively?
I know about the step attribute, the "duplicate answer" doesn't reply to my question.
It could be done by this:
document.getElementById('input').addEventListener('change', force2decimals);
function force2decimals(event) {
event.target.value = parseFloat(event.target.value).toFixed(2);
}
<input type="number" step="0.01" id="input" value="1.00" />
There is no way to do this "natively" in HTML5.
You can use this:
function force2decimals(event) {
var value = $(event).val();
var format_val = parseFloat(value).toFixed(2);
$(event).val(format_val);
}
<input type="number" step="0.01" id="input" onchange="force2decimals(this)" value="1.00" />
I hope this was helpful.
I got old code of my apps that using javascript to calculate something, the logic is if 1 textbox filled, another textbox automatic filled, and when the second textbox filled, that another textbox will return text1 - text2, maybe You can look at this fiddle:
HTML:
<label>Price Start</label>
<input type="number" class="form-control" placeholder="Total Harga Diberikan" required="required" name="price" onkeyup="calculate()" id="price" />
<br />
<label>Discount</label>
<input type="number" class="form-control" placeholder="Nominal Potongan" name="jmlvoucher" onkeyup="calculate()" id="jmlvoucher" />
<br />
<label>Total</label>
<input type="number" class="form-control" placeholder="Total yang harus dibayarkan" required="required" name="total" id="total" />
Javascript:
function calculate(){
var num1 = document.getElementById("price").value;
var num2 = document.getElementById("jmlvoucher").value;
var sum=parseFloat(num1)-parseFloat(num2);
document.getElementById('total').value=sum.toString();
}
http://jsfiddle.net/n4anv5qn/2/
Already try to check error, but there's no error. Try to run it, it doesn't works. Any idea why?
There indeed is an error being generated as keyup is fired.
Uncaught ReferenceError: calculate is not defined
On JSFiddle you've chosen to put your JavaScript into the onLoad function which is wrong. You should instead choose No Wrap
See http://jsfiddle.net/n4anv5qn/5/
As requested, this code is an example of how you can still calculate your Total if your Discount is not filled. Simply check to see if the value you grabbed from num2 is blank and set it to a default value.
function calculate(){
var num1 = document.getElementById("price").value;
var num2 = document.getElementById("jmlvoucher").value;
if (num2 == '')
num2 = '0';
var sum=parseFloat(num1)-parseFloat(num2);
document.getElementById('total').value=sum.toString();
}
You should use onchange instead of onkeyup. Also put your scripts right before the closing body tag.
Here's how to do it:
<label>Price Start</label>
<input type="number" class="form-control"name="price" onchange="calculate()" id="price" />
<br />
<label>Discount</label>
<input type="number" class="form-control" onchange="calculate()" id="jmlvoucher" />
<br />
<label>Total</label>
<input type="number" class="form-control" required="required" name="total" id="total" />
<script>
function calculate(){
var num1 = document.getElementById("price").value;
var num2 = document.getElementById("jmlvoucher").value;
var sum = parseFloat(num1) - parseFloat(num2);
document.getElementById('total').value = sum;
}
</script>
I'm trying to make the minimum allowed input amount to be 1.00 and not to allow 0.99 or anything less than 1.00
<input type="text" onkeyup="switchSlider(this.value, 1)" value="25.00" class="master-amount" name="master-amount" id="master-amount">
User HTML min Attribute
<input type="number" min="1.00" onkeyup="switchSlider(this.value, 1)" value="25.00" class="master-amount" name="master-amount" id="master-amount">
Here is an example for you
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_max_min
Update:
You can test the value on keyup as per code below and force 1 as the min value.
<input type="number" onkeyup="switchSlider(this)" value="25.00" class="master-amount" name="master-amount" id="master-amount">
switchSlider = function (e){
if (!e.value || e.value < 1) e.value=1;
}
http://jsfiddle.net/2dq06qpc/
You can do this right in your markup using the min max and step attributes, note you need to use type="number"
<input type="number" min="0" max="100" step="5">
In your code:
<input min="1" type="number" onkeyup="switchSlider(this.value, 1)" value="25.00" class="master-amount" name="master-amount" id="master-amount">
I have dynamic form like:
<form id="formaa">
<div class="row">
<input type="text" name="item" class="item"></input>
<input type="text" name="quant" class="quant"></input>
<input type="text" name="price" class="price" onkeyup="update();"></input>
<input type="text" name="sum" id="suma" size="10" disabled="disabled"/>
</div>
<div class="row">
<input type="text" name="item" class="item"></input>
<input type="text" name="quant" class="quant"></input>
<input type="text" name="price" class="price" onkeyup="update();"></input>
<input type="text" name="sum" id="suma" size="10" disabled="disabled"/>
</div>
<!-- ... many more rows -->
<input type="text" disabled id="total" name="tot"></input>
</form>
What I'm trying to do is multiply item quantity and price and get total sum in sum field for each item seperately (not all items total sum).
What I have achiecved is this function, but it seems counting total sum of all items together not seperately and this working with first default field row, when I add new set of fields and fill them with information both, fields (and other later added) sum values become NaN..If I remove all added field sets and leave the first form fows set, number is working again.. What is the problem here?
<script type="text/javascript">
function update() {
var total = 0;
$("#formaa div.row").each(function(i,o){
total += $(o).find(".quant").val() *
$(o).find(".price").val();
if(!isNaN(total) && total.length!=0) {
sum += parseFloat(total);
}
});
$("#formaa div.row #suma").val(total);
}
</script>
function update() {
$("#formaa div.row").each(function(i,o){
var total = $(o).find(".quant").val() * $(o).find(".price").val();
if(!isNaN(total) && total.length!=0) {
sum += parseFloat(total);
}
$(o).find('[name="sum"]').val(total);
});
}
A few problems in your code :
never give the same id to more than one element. That's the reason why I defined the selector on the name
you weren't resetting the total, so it was the sum of all
I didn't fix that, but your operation before the float parsing is strange (it may depend on the content that I can't see)