Skip to main content
andrews1111
Participant
Jan 12, 2017 6:16 PM UTC
Answered

Add up values when the check box is selected

I have a form that I am trying to sum up a total of seven lines, but only have them total if the check box next to the value is clicked.  How do I do this?

I do not have them in a table, this is just for easy display of the form I created.

TABLE Example:

CHECK BOXITEMVALUE
XITEM11
XITEM21
XITEM31
XITEM41
XITEM51
XITEM61
XITEM71
DOES NOT HAVE CHECK BOX, JUST A SUM OF CHECKED BOXESTOTALSUM OF ABOVE LINES IF CHECK BOX IS SELECTED
This topic is closed to new replies. Start a new post to keep the conversation going.
Karl Heinz  Kremer
Karl Heinz KremerCorrect answer
Community Expert
Jan 12, 2017 10:29 PM UTC

Let's assume you have your fields named "Line1.chk", "Line1.item", "Line1.value", "Line2.chk" and so on, you can use something like this as the custom calculation script for your "Total" field:

var sum = 0;

for (var i=1; i<=7; i++) {

    if (this.getField("Line" + i + ".chk").value != "Off") {

        sum += Number(this.getField("Line" + i + ".value").value);

    }

}

event.value = sum;

Make sure that you change the number of rows in your form in line #3 in the script (currently set to 7, based on the image you provided).

2 replies

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
Jan 12, 2017 10:29 PM UTC

Let's assume you have your fields named "Line1.chk", "Line1.item", "Line1.value", "Line2.chk" and so on, you can use something like this as the custom calculation script for your "Total" field:

var sum = 0;

for (var i=1; i<=7; i++) {

    if (this.getField("Line" + i + ".chk").value != "Off") {

        sum += Number(this.getField("Line" + i + ".value").value);

    }

}

event.value = sum;

Make sure that you change the number of rows in your form in line #3 in the script (currently set to 7, based on the image you provided).

Translate
andrews1111
Participant
Jan 12, 2017 10:49 PM UTC

Thank you so much!!

Translate