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

Question

Question

Forms that requires addition and division

asked on November 13, 2015 Show version history

Is there a way to add values (1, 2, 3, 4) together, based on user selection? 

Example:

 

Field 1 = 3

Field 2 = 1

Field 3 = 2

Total = 6

 

I am open to any suggestion but the basic principle is that the Field 1, Field 2, and Field 3 will receive user input: a 1, 2, or a 3.

Then the Total would be the sum of the Field 1, Field 2, and Field 3 (whether that is a drop down or radio button or just a text field does not matter too much).

0 0

Answer

SELECTED ANSWER
replied on November 13, 2015

Yes. In that case, you can use the JavaScript example from the Forms web help

$(document).ready(function () {
    $(document).on('blur change', '.sum input', sumtotal);
  
    function sumtotal() {
        var s = 0;
        $('.sum input').each(function () {
            s += parseNumber($(this).val());
        });
        $('.total input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

Have three single line or number fields on the form. Give them all the CSS class name, sum. Then have a fourth field with the CSS class name, total.

Now as users enter in values into those first three fields, the fourth field will be updated with the sum.

2 0

Replies

replied on November 13, 2015

Hi Lidija,

Can you provide more details regarding your request? Is your form only going to have three fields that users will be inputting various values into and you just want to have a fourth field display the total? Or would this be part of a table where users can add/remove as many rows as they want? If you can clarify the use case, then a more meaningful suggestion can be provided.

Regards

0 0
replied on November 13, 2015

Alexander, thank you for your reply, sorry for not being clear.

I am open to any suggestion honestly but the basic principle is that the Field 1, Field 2, and Field 3 will receive user input: a 1, 2, or a 3. Then the Total would be the sum of the Field 1, Field 2, and Field 3 (whether that is a drop down or radio button or just a text field does not matter too much). Does that make more sense?

0 0
SELECTED ANSWER
replied on November 13, 2015

Yes. In that case, you can use the JavaScript example from the Forms web help

$(document).ready(function () {
    $(document).on('blur change', '.sum input', sumtotal);
  
    function sumtotal() {
        var s = 0;
        $('.sum input').each(function () {
            s += parseNumber($(this).val());
        });
        $('.total input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

Have three single line or number fields on the form. Give them all the CSS class name, sum. Then have a fourth field with the CSS class name, total.

Now as users enter in values into those first three fields, the fourth field will be updated with the sum.

2 0
replied on November 19, 2015

Thanks!!! Didn't know we had those guides there

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

Sign in to reply to this post.