Hello,
I am trying to find a way to multiply two numeric values (with two fields with the CSS class “field-one” and “field-two” and output the result in a 3rd field with the CSS class “result”. However, I want to do this in text fields (not number fields). I cannot use the integrated calculation because it is only available in number fields.
I am using this script here:
<script>
$(document).ready(function(){
// Function for removing thousand separators
function removeSeparators(value) {
return value.replace(/[.,]/g, '');
}
// Function for multiplying the values and displaying the result
function calculateMultiplication() {
// Retrieve the values of the input fields and remove the thousands separator
var value1 = removeSeparators($('.field-one').val());
var value2 = removeSeparators($('.field-two').val());
// Convert values to numbers
var num1 = parseFloat(value1);
var num2 = parseFloat(value2);
// Check whether the values are valid numbers
if (!isNaN(num1) && !isNaN(num2)) {
// Perform multiplication
var result = num1 * num2;
// Display the result in the result field
$('.result').val(result);
} else {
// If the values are not valid numbers, empty the result field
$('.result').val('');
}
}
// Calculation when changing the input fields
$('.field-one, .field-two').on('input', calculateMultiplication);
});
</script>
When I paste this script into an HTML file, everything works fine, but when I paste it into a Gravity Forms form, it doesn’t work.
How can I achieve this?
Thanks for help,
Michael