The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

If function in calculation field

Labels

This Discussion is public

Notifications

I am trying to make a form where calculation results are depending on a value X in a field. The results are for example 20 when value X is between 0 and 7, 30 when value X is between 7 and 12 and 40 when value X is between 12 and 18. Is this possible in a number field, for example with an IF function?

Formidable's calculations can perform any combination of mathematical tasks as long as the function is in the JavaScript Math Object.

Complex calculations like if statements are best handled through jQuery. You'll need to write your own function(s) and store the result in another field.

Hi

I need a similar function. I'm basically calculating a rate based on monthly or annual selected in a dropdown field.

I'm really struggling with the syntax. my JavaScript works if it put an actual value in i.e.

IF 'Monthly' field = '0.0412' / '12'

IF 'Annual' field = '0.0412' ,

but not if it try to call a value in from another field i.e.

IF 'Monthly' field = '[211]' / '12'

IF 'Annual' field = '[211]' ,

Did you have any luck? This is my JavaScript

<script type="text/javascript"> // Sets the rate

jQuery(document).ready(function($){
$('select[name="item_meta[216]"]').change(function(){
var val1 = $("select[name='item_meta[216]']").val();
if (val1 == '12')
{$("#field_rate").val('[211]' / '12');}    // Doesn't work
else if (val1 == '1')
{$("#field_rate").val('0.00412');}    //Works
$("#field_rate").change();
});
});
</script>

You can't use shortcodes, i.e., [211], within a jQuery function. Shortcodes are executed on the server in PHP. jQuery is executed in the browser only.

To access the value of a field in jQuery, store it in a var as you have with val1.

Thanks. Would I set the var like this

var val2 = $("select[name='item_meta[211]']").val();

var val3 = $("select[name='item_meta[211]']").val();

var tot = val2 / val3

 

then finish off with

{$("#field_rate").val('tot');}

i've left out the rest of the code

In general, yes. But, the way you have your code, val2 and val3 would have identical values so tot would always = 1.

Hi vfontjr

I've tried this and it doesn't work. I've also tried it without the $('select[name="item_meta[211]"]').change(function(){

That doesn't work either. I've done a lot of JavaScript in Adobe PDF forms, but this is completely different :(
<script type="text/javascript">
jQuery(document).ready(function($){
$('select[name="item_meta[216]"]').change(function(){
var val1 = $("select[name='item_meta[216]']").val();
$('select[name="item_meta[211]"]').change(function(){
var val2 = $("select[name='item_meta[211]']").val();

if (val1 == '12')
{$("#field_rate").val('val2' / '12');} // Doesn't work
else if (val1 == '1')
{$("#field_rate").val('0.00412');} //Works
$("#field_rate").change();
});
});
</script>

Yes that was a typo (copy & paste error)

But it just returns ""

does it make any difference what type of field it is?

the data is coming from dropdown fields and the result is a text field (number)

The problem is scoping. Your val1 and val2 values are embedded in the change functions for the two selects, then you run code that is not part of either function and expect it to return values. This will never work because val1 and val2 are not visible to the out of function code. I would just use something like this:


jQuery(document).ready(function ($) {
"use strict";
$('select[name="item_meta[216]"], select[name="item_meta[211]"]').on('change', function() {
var val1 = $("select[name='item_meta[216]']").val(),
val2 = $("select[name='item_meta[211]']").val(),
tot = val1 / val2;

$("#field_rate").val(tot);
});
});

Obviously, I can't test it because I don't have access to your form, but may it will help.

can we speak next week. it might be easier if you just build what I'm trying make.

Where are you based?

I'm in Raleigh, NC. US Eastern time. You can call on my toll-free number. 1-844-842-3668.

Discussion closed.