Skip to main content
Participant
February 9, 2022
Answered

Count checked boxes leave blank if 0

  • February 9, 2022
  • 1 reply
  • 366 views

I am very new to javascript.  I learned what I do know right here LOL

I have a form with Monday to Sunday. I have checkboxes for each day.  I figured out how to count the checkboxes and return the total number of days checked. However, it puts a 0 when there are none checked. How can I edit my script so it will leave the total blank?

 

// Initialize counter variable
var sum = 0
// Loop through the fields
for (var i = 1; i < 8; i += 1) {
// Add one if check box is not Off
if (getField("a" + i).value !== "Off") {sum += 1;}
}
// Set this field's value to the sum;
event.value = sum;

This topic has been closed for replies.
Correct answer Bernd Alheit

At the end add this:

if (sum == 0) event.value = "";

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
February 10, 2022

At the end add this:

if (sum == 0) event.value = "";

RJ873Author
Participant
February 10, 2022

You are awesome!  That worked. Thank you so much!!