Two fields do a calculation

Can you make two field do math dynamically in the form? for example, quality is 5 and the unit price is 5.00
the total would be 25.00

Hello @jktodd007,

Yes, you can add the on change event on the Price field and do any calculation. Please see the code sample below.

fd.spRendered(function() {
fd.field('price').$on('change', function() {

      var el1 = parseInt(fd.field('quantity').value);
      var el2 = parseInt(fd.field('price').value);

      fd.field('Total').value = el1 * el2;
});
});

Is there a way to do this with the below data?
image

that even if B or C or D or E is left blank it will still calculate with F.

The formula of the calculation is F = A - (B+C+D+E)

Hello @Jamail_Serio,

Yes, you can use the same approach. Please see teh code sample below.

function calculation() {
    var fieldA = 0;
    var fieldB = 0;
    var fieldC = 0;

    if(fd.field('FieldA').value) {
        var fieldA = fd.field('FieldA').value;
    }

    if(fd.field('FieldB').value) {
        var fieldB = fd.field('FieldB').value;
    }
    if(fd.field('FieldC').value) {
        var fieldC = fd.field('FieldC').value;
    }


    fd.field('FieldF').value = fieldA - (fieldB + fieldC);
}

calculation();

fd.field('FieldA').$on('change', calculation);
fd.field('FieldB').$on('change', calculation);
fd.field('FieldC').$on('change', calculation);

Please use fields' internal names in the code.

1 Like

I added this code to my form (JSS) and it doesn't do anything. Any ideas?

fd.spRendered(function() {
fd.field('Registration_x0020_Cost').$on('change', function() {

  var el1 = parseInt(fd.field('Registration_x0020_Days').value);
  var el2 = parseInt(fd.field('Registration_x0020_Cost').value);

  fd.field('Registration_x0020_Cost_x0020_To').value = el1 * el2;

});
});

Hello @eweiler,

What forms are you using? Public or SharePoint forms?

Are you getting any errors in the browser's console? Please share the screenshot.

This is SharePoint forms for OS365

here is the screen shot of the errors in the console

@eweiler,

Thank you!

Please export the form and share it with me. Thank you!

Item (Edit).json (26.2 KB)

@eweiler,

Thank you!

What is the type of Registration_x0020_Cost_x0020_To and Registration_x0020_Cost fields?

Update the code like so and check the errors in the browser console:

fd.spRendered(function() {
    fd.field('Registration_x0020_Cost').$on('change', calculation);
    fd.field('Registration_x0020_Days').$on('change', calculation);
    
    calculation();
    
    function calculation() {
        var el1 = parseInt(fd.field('Registration_x0020_Days').value);
          var el2 = parseInt(fd.field('Registration_x0020_Cost').value);
    
          fd.field('Registration_x0020_Cost_x0020_To').value = el1 * el2;
    }
});

That worked.. thank you so much

Registration_x0020_Cost is is a currency field
Registration_x0020_Days is a number field
Registration_x0020_Cost_x0020_To is a currency field