Skip to main content
Kris Hunt
Legend
January 20, 2024
Question

Change global variables based on radio button selection

  • January 20, 2024
  • 1 reply
  • 1715 views

I have a real simple problem: I have a form with a radio button group called "Membership Status". The two options are "Member" and "Non-Member". When "Member" is selected, I want to change the values of a set of global variables one way, and when "Non-Member" is selected, I want to change the values another way. Where do I put this code (e.g., as a Javascript action on each separate radio button option, on a document Javascript, etc), and what's the syntax to get me started?

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
January 20, 2024

I would use the Calculation script of a (hidden) text field to do it. Like this:

 

var memStatus = this.getField("Membership Status").valueAsString;
if (memStatus=="Member") {
	global.var1 = "ABC";
	global.var2 = 1234;
} else if (memStatus=="Non-Member") {
	global.var1 = "DEF";
	global.var2 = 5678;
} else {
	global.var1 = "";
	global.var2 = 0;
}

 

The reason for doing it like that is that it would also update if the form is cleared, for example, which a Mouse Up script on the radio-buttons themselves won't do.

 

Kris Hunt
Kris HuntAuthor
Legend
January 20, 2024

When would this script calculate? As soon as the PDF is opened? Would it recalculate each time the radio button choice changes?

try67
Community Expert
Community Expert
January 20, 2024

Each time the value of any field in the file is changed. If you want it to also update when the file is opened add this command under a doc-level script (NOT in a function):

 

this.calculateNow();