Forums

Sum some fields number

BASSI 17 Jun, 2015
Hi,
I created a form as a software where users could put some information. I'd like to create some fields "test1, test2, test3" as numbers, and then create the field "testSum" where I have the sum of the test fields.
Could you help me, please?
Thank you in advance.
Vito
GreyHead 17 Jun, 2015
Hi BASSI,

You can do this using JavaScript in a Load JavaScript action in the form On Load event. Here's an example
jQuery(document).ready(function(jQ) {
  var box_1, box_2, box_3, total;
  jQ('#box_1').keyup(calculateTotal);
  jQ('#box_2').keyup(calculateTotal);
  jQ('#box_3').keyup(calculateTotal);

  function calculateTotal() {
    total = 0;

    box_1 = jQ('#box_1').val();
    if ( isNaN(box_1) || !box_1 ) {
      box_1 = 0;
    }
    total += parseFloat(box_1);

    box_2 = jQ('#box_2').val();
    if ( isNaN(box_2) || !box_2 ) {
      box_2 = 0;
    }
    total += parseFloat(box_2);

    box_3 = jQ('#box_3').val();
    if ( isNaN(box_3) || !box_3 ) {
      box_3 = 0;
    }
    total += parseFloat(box_3);

    jQ('#total').val(total);
  }
});

Bob
BASSI 17 Jun, 2015
I'm sorry I don't know JS script. I tried to put it on "setup" load Javascript, also I change the names of the var as the fields name, and then I call the function name for the three fields, but it doesn't work. Probably I lost a passage... 🙂
BASSI 17 Jun, 2015
Hi Grey, could you explain how I can obtain the result? Thank you very much.
Vito
GreyHead 18 Jun, 2015
Hi Vito,

The JavaScript uses the element IDs, not the names (it helps if they are the same).

Check this, if that doesn't work you probably need help from someone with more JavaScript experience.

Bob
BASSI 19 Jun, 2015
Hi Bob,
Thank you very much,
I solved the problem, now the sum works with your Javascript code.
Regards,
Vito
This topic is locked and no more replies can be posted.