How to multiply price and tax percentage to get tax amount automatically with javascript?
-
28-10-2019 - |
سؤال
I want to multiply price and tax percentage to get tax amount automatically with javascript. After I input the price and tax percentage, the text field of tax amount automatically changes into the result of calculation. I developed my web with php and javascript.
Price : $<input type="text" class="price" value=""><br/>
Tax (%) : <input type="text" class="dp_per" value="">%<br/>
Tax Amount : $<input type="text" class="dp_per" value=""><br/>
<br/>
المحلول
You can get the values with className and do the math. I used getElementsByClassName
to match your posted code, but I would give each of these id's and use getElementById
instead. I bound to the onblur event of the tax as you did not provide any specifications should trigger a recalc of the taxAmount. Make sure that you check this server side too as bypassing client side javascript is trivial.
http://jsfiddle.net/mrtsherman/97hnD/
var price = document.getElementsByClassName('price')[0];
var tax = document.getElementsByClassName('dp_per')[0];
var taxAmount = document.getElementsByClassName('dp_per')[1];
tax.onblur = function() {
taxAmount.value = parseFloat(price.value) * parseFloat(tax.value) / 100;
}