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

Question

Question

How to Divide sum of Field Values by Number?

asked on July 30, 2015

I have taken the code provided in the help documentation and have the following function:

$('.cf-table-block').on('blur change', '.domain3Sum select', domain3sumtotal);
  
    function domain3sumtotal() {
        var s = 0;
        $('.domain3Sum select').each(function () {
            s += parseNumber($(this).val());
        });
        $('.domain3Subtotal input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }

It calculates the number correctly. Now I need to take the sum of the .domain3Sum fields and divide them by specific number like 3. How do I alter the code to do that and then put that in the .domain3Subtotal field?

0 0

Answer

SELECTED ANSWER
replied on July 30, 2015
$('.cf-table-block').on('blur change', '.domain3Sum select', domain3sumtotal);
  
    function domain3sumtotal() {
        var s = 0;
        $('.domain3Sum select').each(function () {
            s += parseNumber($(this).val());
        });
            s = s/3;
        $('.domain3Subtotal input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }

Since it's storing the sums in the variable s, and then assigning "s" to be the value of the input field, just insert the dividing step you mentioned like so.

0 0
replied on July 30, 2015

I was so close. Thank you for your help.

0 0

Replies

replied on December 15, 2015

I'm trying to adapt the code written above to help me divide two field values (entered into the form by the user) and place the result in another form field. 

 

Can you provide some guidance?  The values are Single Line Fields (not in a table). 

0 0
replied on December 15, 2015

Can try this. Make sure the number you want to divide has a CSS class of dividend set to dividend, the divisor set to divisor, and I'm outputting the result into another single line field, with a css class of quotient.

 

$(document).ready(function(){

  $('.divisor').on('change', divide);

    function divide() {
        var a = parseNumber($('.dividend input').val());
        var b = parseNumber($('.divisor input').val());
        var c = a/b;
            
        $('.quotient input').val(c);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
      
    }
});

 

0 0
replied on December 15, 2015

This worked great, thank you so much!

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

Sign in to reply to this post.