Calculate price and TVA with jquery
-
20-12-2019 - |
Question
I have this html and JQuery code: jsfilddle
My Jquery code:
$("select#edit-field-tva-percentage-und").on('change', function () {
var VATpercentage = $("select#edit-field-tva-percentage-und option").filter(":selected").text();
alert(VATpercentage);
});
jQuery("#edit-field-prezzo-und-0-value").keyup(function () {
jQuery("#edit-field-totale-documento-und-0-value").val(
jQuery("#edit-field-prezzo-und-0-value").val()
);
});
My question is: How I can show in real time the calculation of the price and TVA in the Total field?
Solution
Here is a simple working example :
HTML
<select id="vta">
<option value="21">21</option>
<option value="15">15</option>
<option value="12">12</option>
<option value="10">10</option>
</select>
<div class="form-item form-type-textfield form-item-field-prezzo-und-0-value">
<label for="edit-field-prezzo-und-0-value">Price</label>
<input type="text" class="form-text" maxlength="10" size="12" value="" name="field_prezzo[und][0][value]" id="edit-field-prezzo-und-0-value">
Total <input type="text" id="total" >
JS
$(function() {
// Handler for .ready() called.
$("#vta").on('change', function () {
updatePrice();
});
$("#edit-field-prezzo-und-0-value").keyup(function () {
updatePrice();
});
});
function updatePrice(){
var novta = $("#edit-field-prezzo-und-0-value").val();
var vta = $("#vta").val();
var total = novta*vta;
$("#total").val(total);
}
(EDIT) Working example here : http://jsfiddle.net/kELm3/12/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow