Hi @OOlashyn and @OliverRodrigues ,
I have tried adding the reverse clause that Oliver mentioned and it worked, the value you get when accessing the fields value is not the one I am visualizing because when introducing the number I can't introduce the "," but it works, I just have to divide it by 100.
Regarding the "oninput" event for the "iva" field (not the masked inputs I created), it is not firing when introducing the value. What I am trying is to calculate a "total" value from a multiplication of the values of the fields "base" and "iva". I want that when any of these two values are updated to recalculate the "total" value. What I have accomplished with the following code is to be able to calculate the "total" value only when the "base" field is updated by the user. If the user updates the "iva" value nothing happens, it looks like the "oninput" event is not linking well to that field.
Another funny thing that happens is that the mask for the "total" field only fires when the user modifies the input directly, is there a way to also apply the mask when I modify the value through JavaScript?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code:
$(document).ready(function() {
let baseImponible = "#cr027_baseimponiblenumber";
let importeTotal = "#cr027_importe";
let iva = "#cr027_iva";
//hide total field
$(importeTotal).hide();
//Create masked field for total
let maskedTotal = document.createElement("input");
maskedTotal.className = "form-control";
maskedTotal.id = "maskedTotal";
//add function to Total to set its mask when receiving input
maskedTotal.oninput = () => {
console.log("Masked total");
let newValue = parseFloat($("#maskedTotal").val().replace(/\D/g, ''), 10) / 100;
$(importeTotal).val(newValue);
}
//apply mask to total
$(maskedTotal).mask("###.##0,00", {reverse: true});
//insert masked total after original total
$(maskedTotal).insertAfter($(importeTotal));
//hide base field
$(baseImponible).hide();
//create new masked base
let maskedBase = document.createElement("input");
maskedBase.className = "form-control";
maskedBase.id = "maskedBase";
//add function to base to set its mask and to calculate the total using the field "iva"
maskedBase.oninput = () => {
console.log("Masked base");
let newValue = parseFloat($("#maskedBase").val().replace(/\D/g, ''), 10) / 100;
$(baseImponible).val(newValue);
let calculatedValue = (1 + parseInt($(iva).val().replace(/\D/g, ''), 10) / 100) * newValue;
$(maskedTotal).val(calculatedValue.toFixed(2));
}
//apply mask to base
$(maskedBase).mask("###.##0,00", {reverse: true});
//insert masked base
$(maskedBase).insertAfter($(baseImponible));
//Calculate total when the "iva" field is introduced also (we also calculate it when the base is introduced)
$(iva).oninput = () => {
console.log("Iva On Input started");
let newValue = parseFloat($("#maskedBase").val().replace(/\D/g, ''), 10) / 100;
console.log("newValue: ", newValue);
let calculatedValue = (1 + parseInt($(iva).val().replace(/\D/g, ''), 10) / 100) * newValue;
console.log("calculated Value: ", calculatedValue);
$(importeTotal).val(calculatedValue);
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks for all the help!