• Resolved zukenstein

    (@zukenstein)


    Hi I want to calculate a value in one field if a value is entered into another, but also do it the other way to, so for instance
    the calculation in field 1

    (function(){
    if(fieldname2 >= 1) return fieldname2*3;
    })();

    But also in field 2 if someone enters a value in field 1 instead, this calculation in field 2

    (function(){
    if(fieldname1 >= 1) return fieldname1*5;
    })();

    How should I do this so I don’t get stuck in an infinite loop? Just adding those equations they will keep triggering each other wont they?

    Thanks

    • This topic was modified 4 years, 3 months ago by zukenstein.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @zukenstein

    You might need to implement some tricks.

    Insert a “HTML Content” field in the form with the following piece of code as its content:

    
    <script>
    var edited_field = 0;
    jQuery(document).on('keydown', '[id*="fieldname1_"]', function(){edited_field = 1;});
    jQuery(document).on('keydown', '[id*="fieldname2_"]', function(){edited_field = 2;});
    </script>
    

    and then, edit the equations as follows:

    
    (function(){
    if(edited_field == 2) return fieldname2*3;
    return __ME__;
    })();
    

    and

    
    (function(){
    if(edited_field == 1) return fieldname1*5;
    return __ME__;
    })();
    

    Best regards.

    Thread Starter zukenstein

    (@zukenstein)

    Thats excellent thanks

    One further thing please, what would I do to introduce another field 3 as an option so

    Field 1 how you said

    (function(){
    if(edited_field == 2) return fieldname2*3;
    return __ME__;
    })();

    If field 3 has a value entered but not field 2, how would I add

    if(edited_field == 3) return fieldname3*7;

    into the above equation? I tried

    (function(){
    if((edited_field == 2) return fieldname2*3) || ((edited_field == 3) return fieldname3*7);
    return __ME__;
    })();

    But that didn’t seem to work thanks

    • This reply was modified 4 years, 3 months ago by zukenstein.
    Thread Starter zukenstein

    (@zukenstein)

    BTW I did also add field 3 into the script

    Plugin Author codepeople

    (@codepeople)

    Hello @zukenstein

    Your use of the parenthesis is wrong, the equation would be:

    
    (function(){
    if(edited_field == 2) return fieldname2*3;
    else if(edited_field == 3) return fieldname2*7;
    return __ME__;
    })();
    

    Best regards.

    Thread Starter zukenstein

    (@zukenstein)

    Ok thanks very much for that, great plugin, awesome support ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Loop of calculations?’ is closed to new replies.