Skip to main content
Known Participant
Sep 27, 2020 8:54 PM UTC
Answered

Check Box Value calculation

I have 4 checkbox fields check1,check2...etc all has export value of 1. If I use SFN or value is, it calculates them correctly but I can't figure out how to calculate values  in custom calculation script.

This topic is closed to new replies. Start a new post to keep the conversation going.
George_JohnsonCorrect answer
Inspiring
Sep 28, 2020 3:17 PM UTC

Sorry I thought it will be clear from the code, I can't use that because total is from text fields not checkboxes, when checkbox is checked it add text field value to total.There is 4 checkboxes and 4 text fields (5th been where code go).


Thanks for the clarification. You will have to add a variable to keep track of how many check boxes are checked, and you should explicitly convert the text field values to numbers in case any are blank. Something like this:

 

var total = 0;
var cb_total = 0;

for (var i = 1; i <= 4; i++) {
    if (getField("checkbox"+i).value == "1") {
        cb_total += 1;
        total += +getField("checkbox" + i + "-price").value;
    }
}

event.value = cb_total > 1 : 0.8 * total : total;

8 replies

Inspiring
Sep 28, 2020 1:22 AM UTC

Can you explain what type of calculation you want to perform? It is a simple sum of the values of the checkboxes, or something else. If a simple sum, why do you want to use a custom script? In any case, with a script you can retrieve the value of each, ignore any that have a value of "Off", and add those that are not, after converting them to a number.

Translate
MarietaaAuthor
Known Participant
Sep 28, 2020 8:12 AM UTC

I want to add to folowing script if 2 of the checkboxes are checked total*.80

var total = 0;
for (var i=1; i<=4; i++) {
if (this.getField("checkbox"+i).value=="1") {
total+=this.getField("checkbox"+i+"-price").value;
}
}
event.value = total;

Translate
Inspiring
Sep 28, 2020 2:47 PM UTC

Then that last line could be:

 

event.value = total > 1 ? 0.8 * total : total;

Translate