You are viewing limited content. For full access, please sign in.

Question

Question

Forms adding multiple fields

asked on January 25, 2016

I'm trying to find the sum of multiple different fields. They all have different CSS Classes. Can anyone give me the start of this script? I'm running into trouble I found the script for tables but I can't get it to work for fields.

0 0

Answer

SELECTED ANSWER
replied on January 25, 2016 Show version history

Hi Chynna,

Your subtotal fields are being added together as strings, not numbers ('1'+'20'+30' = '12030'). In Forms 10, field calculations are available out of the box, no coding needed. In Forms 9, you need to use a parseNumber function to change the strings to numbers:

function parseNumber(n) {
  var f = parseFloat(n); //Convert to float number.
  return isNaN(f) ? 0 : f; //treat invalid input as 0;
}

The way your code is set up, you would need to place each of your values inside of this function (where 'n' is on line 1). However, if you give each of your Subtotal fields the 'subtotal' class, the code would be be much easier to manage. You can add classes to fields by typing them into the "CSS class" section of the field's advanced tab. To add multiple classes to a field, just separate them by a space. For example, I have assigned the following field three different classes:

If you assign the "subtotal" class to each of your Subtotal fields, then the following code would be one way to achieve your goal:

$(document).ready(function(){
  $('#q73 input').prop('readOnly', true); //Set Total to Read-Only
  $(document).on('change', '.subtotal input', function(){ //When a Subtotal field changes...
    var total = 0;
    $('.subtotal input').each(function(){ //For each Subtotal input...
      var subtotal = parseNumber($(this).val()); //Convert the value to a number
      total += subtotal; //Add all the numbers up
    });
    $('#q73 input').val(total); //Put the result into the Total field
  });
  function parseNumber(n){
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
});

Let me know if this works for you!

1 0

Replies

replied on January 26, 2016

Awesome!!!!!! This works great! Thanks for the help! Still getting learning CSS and Javascript and this was a big help for a lot of Forms!

1 0
replied on January 25, 2016

});

$(document).ready(function () { 
  $('#q30 input, #q41 input, #q45 input, #q50 input, #q51 input, #q61 input, #q66 input, #q71 input').blur(function () {
    $('#q73 input').val($('#q30 input').val() + $('#q41 input').val()+ $('#q45 input').val()+ $('#q50 input').val()+ $('#q51 input').val()+ $('#q61 input').val()+ $('#q66 input').val()+ $('#q71 input').val());
  });
});

 

This what I have so far....It's just putting the number in the total field. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.