Question

I'm trying to add a formula to my gravity form (WordPress) which includes exponent.i cant use the build in calculator since there is no exponent function. its basically a loan calculator where.

r=rate ;(form_id=4)/12

p=loan amount ;(form_id=1)

D=duration ;(form_id=2, [drop down menu]) * 12

X=p(r/1-(1+r)^-D)

X= repayment

Now I get the r, p and D from the form in form of drop down and written numbers.

so far I've put a HTML box and inserted this code in it :

<script>
gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){    
if ( formulaField.field_id == "2" ) {
    result = /****/;
}
return result;
});
</script>

I don't have any programming bg and I got it from a support team member.

What I understand is that this code will replace the calculation of field_id 2 (which is showing the X=[monthly repayment] with the result of my formula -> "/****/"

But i have to write the formula and i don't know how to get the values from the form with jQuery and put them in the formula.

I found out JavaScript math functions from W3school and another website so i don't have problem with writing the formula i just need help with jQuery to get the value.

I was wondering if someone can help me with that?

Was it helpful?

Solution

You can grab the field values using jQuery .val(). So if you wanted to get the value for field 5 you could use something like this

var field_five = jQuery('#input_2_5').val();

Input id's follow the naming convention of input_{form_id}_{field_id} and for multi input fields they will end with _{input_no}. You can confirm what the input id is by inspecting the field using the browsers developer tools where you would see something like this.

<li id="field_2_5" class="gfield">
    <label class="gfield_label" for="input_2_5">Number</label>
    <div class="ginput_container">
        <input name="input_5" id="input_2_5" type="text" value="" class="medium" tabindex="5">
    </div>
</li>

To use the retrieved value in the calculation the script would look like this:

<script>
gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){    
    if ( formulaField.field_id == "2" ) {
        var field_five = jQuery('#input_2_5').val();
        result = field_five * 12;
    }
    return result;
});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top