Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Radio Buttons Based on Text Value in Loop

Participant ,
May 23, 2025 May 23, 2025

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

 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
465
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
May 23, 2025 May 23, 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;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2025 May 27, 2025

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";

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2025 May 23, 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;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 23, 2025 May 23, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2025 May 23, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 24, 2025 May 24, 2025

Ah okay.  So how does {checkField = "Choice1"} work then?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2025 May 24, 2025

While functional, this script treats empty fields as 0. That means "Choice2" will still be selected even when all fields are empty, which might not be the desired behavior.

If you want to avoid setting any checkbox value when fields are empty, you could add a check to skip empty fields or only apply the logic if at least one field has a valid number.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2025 May 27, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2025 May 27, 2025

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";
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 02, 2025 Jun 02, 2025
LATEST

Thank you sir that works but how does that code work?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2025 May 25, 2025

It sets the value of the variable checkField to "Choice1".  The last line sets the value of the checkbox to the value of the checkField variable.

var checkboxField = this.getField("CheckBox");//define variable for CheckBox field object
var checkField="Choice2";//Set its default value to Choice2
for (var i=1; i<=20; i++) {//Loop through fields and obtain values (prices)
	var num = (i < 10 ? "0" + i : i);
	var price = Number(this.getField("Price" + num).valueAsString);
	if (price >= 5000) {checkField = "Choice1"; break;}//if price is >= 5000, set checkField Choice2 and stop the loop
}
checkboxField.value = checkField;//set CheckBox value to checkField variable
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 25, 2025 May 25, 2025

Did you mean:

//if price is >= 5000, set checkField Choice1 (not Choice2) and stop the loop
}

 

In other words, if it isn't Choice1, it will stop the loop and revert back to the default value which is Choice2?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2025 May 25, 2025

Yes

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2025 May 27, 2025

Thank you sir.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines