Skip to main content
andrews1111
Participant
January 12, 2017
Answered

Add up values when the check box is selected

  • January 12, 2017
  • 1 reply
  • 792 views

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 has been closed for replies.
Correct answer Karl Heinz Kremer

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).

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
January 12, 2017

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).

andrews1111
Participant
January 12, 2017

Thank you so much!!