Skip to main content
Known Participant
January 29, 2024
Answered

Calculate total fee based on number of selected checkboxes & fee amount fields

  • January 29, 2024
  • 2 replies
  • 845 views

 

Hi, I have a form with fields that states the fees that are based on the mumber of runs you select. There are checkboxes for each day for you to select the runs you want to do. You can select any combination of number & type of runs each day. If you do 1 run, the amount is one amount, your second run is a lower amount, your third run is a lower amount, & so-forth.

 

I can't assign fee amounts as the values to the checkboxes, because it is dependent on how many runs you do - and you you may not always choose STD as your first run & JWW as your second run, etc - you may choose JWW as your first run & FAST as your second run, etc.

 

I also can't just put the fee amounts in my entry fee calculation, because the fee amounts may change dependending on who is hosting the event - so I need my calculation to get the value entered in those fee fields, based on the the number of checkboxes checked by day, & total the days.

 

I'm guessing the logic of my calculation needs to be something like...

 

Day 1 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 2 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 3 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc) + Day 4 (ifs... checkboxes for the day total 1, then fee field 1, checkboxes for the day total 2, then fee field 1 +  field 2, checkboxes for the day total 3, then fee field 1 + field 2 + field 3, etc)

 

... but I don't know how to do that in JS. Also, does there need to be something to prevent an error if there isn't a Day 4, or isn't a fee in the 5th/6th runs for that event? Does there need to be something in the calculation to show "$0.00" if nothing is checked?

Any help would be most appreciated - thanks!

This topic has been closed for replies.
Correct answer Nesa Nurani

Twice 🙂


Use this:

var total = 0;
var n1 = Number(this.getField("1st").valueAsString);
var n2 = Number(this.getField("2nd").valueAsString);
var n3_4 = Number(this.getField("3rd & 4th").valueAsString);
var n5_6 = Number(this.getField("5th & 6th").valueAsString);

function incrementD(index) {
 var d = 0;
 ['STD', 'JWW', 'FAST', 'T2B', 'PremS', 'PremJ'].forEach(function(event) {
 if (this.getField(index + " " + event).valueAsString !== "Off") {
 d++;}}, this);

if (d >= 1) total += n1;
if (d >= 2) total += n2;
if (d >= 3) total += n3_4;
if (d >= 4) total += n3_4;
if (d >= 5) total += n5_6;
if (d == 6) total += n5_6;}

for (var i = 1; i <= 4; i++) {
 incrementD(i);}

event.value = total;

2 replies

Nesa Nurani
Community Expert
Community Expert
January 29, 2024

What are the field names or if you can share a file with us?

You want to show one total for all, or totals for each day separate?

Known Participant
January 29, 2024

Checkbox names:

(+ same for Day 3 & Day 4)

 

Fee field names:

One total box for all days, field with text above "Entry Fee Enclosed" in original screenshot, field name "Fee".

Nesa Nurani
Community Expert
Community Expert
January 30, 2024

If checkboxes for the day are 4 does it calculate 3rd & 4th twice or only once?

Known Participant
January 29, 2024

This reply was originally to correct the typos in my original post, but I now have the option to edit my posts, so I edited my OP to remove the typos & this reply is no longer needed.