Skip to main content
Inspiring
May 24, 2025
Answered

Radio Buttons Based on Text Value in Loop

  • May 24, 2025
  • 1 reply
  • 1216 views

The following calculation script works to loop through 20 fields and if 5000 value is present, it will check Choice1 (yes) but when it is less than 5000, it won't check Choice2 (no).

var checkboxField = this.getField("CheckBox");
var checkField = false;
for (var i=1; i<=20; i++) {
	var num = (i < 10 ? "0" + i : i);
	var price = Number(this.getField("Price" + num).valueAsString);
	if (price >= 5000) {checkField = true; break;}
}
checkboxField.checkThisBox(0, checkField);

 

Correct answer Nesa Nurani

Yes it is the desired behavior.  But since you mention it, how would you skip empty fields or apply logic only if at least one field has a valid number?  Might be nice to know for future.


You can use this:

var cb = this.getField("CheckBox"),b=0,s=0;
for(var i=1; i<=20; i++){
 var v = this.getField("Price" + (i<10 ? "0" + i:i)).valueAsString;
  if(v && !isNaN(v)) +v >= 5000 ? b=1:s=1;}
cb.value = b ? "Choice1" : s ? "Choice2":"Off";

1 reply

PDF Automation Station
Community Expert
Community Expert
May 24, 2025

The zero in your last line is only operating on the first widget of the check box series, so it will check or uncheck the first widget, depending on the value of checkField.  Try setting the value of the field instead of checkThisBox like this:

var checkboxField = this.getField("CheckBox");
var checkField="Choice2";
for (var i=1; i<=20; i++) {
	var num = (i < 10 ? "0" + i : i);
	var price = Number(this.getField("Price" + num).valueAsString);
	if (price >= 5000) {checkField = "Choice1"; break;}
}
checkboxField.value = checkField;
MBChelsAuthor
Inspiring
May 24, 2025

So Choice2 is the false argument?  So it loops and if any fields are 5000 or greater, it will trigger Choice1.  If not, it breaks, skips below and checkboxField.value then becomes equal to checkField triggering Choice2?

PDF Automation Station
Community Expert
Community Expert
May 24, 2025

Almost.  The loop breaks as soon as price >= 5000 triggers Choice1.  If it is never triggered, checkField remains as Choice2.