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

Change global variables based on radio button selection

Mentor ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

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?

TOPICS
How to , JavaScript , PDF forms

Views

306

Translate

Translate

Report

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 ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

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
Mentor ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Mentor ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

Thank you very much!

Votes

Translate

Translate

Report

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 ,
Jan 20, 2024 Jan 20, 2024

Copy link to clipboard

Copied

You should also know that global variables are protected in the scripting model.  The form will only be able to set and get the variable that is has already set. If you save the form in a way that changes the DocID, these variable will not be accessible in that copy. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Jan 21, 2024 Jan 21, 2024

Copy link to clipboard

Copied

Are you sure about that? Doesn't that defeat the whole point of a global variable?

Votes

Translate

Translate

Report

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 ,
Jan 21, 2024 Jan 21, 2024

Copy link to clipboard

Copied

It's one of the preference settings in the JavaScript Category, since Acrobat 9 I think. 

I'm not sure if changing the minor part of the docID  will block global varible access. But I know changing the major part will.  

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Jan 21, 2024 Jan 21, 2024

Copy link to clipboard

Copied

LATEST

Ah, you mean the "Enable global object security policy" box? I always instruct my clients to turn it off whenever I have a script that uses global variables. Without doing that it's pretty much useless, indeed.

Votes

Translate

Translate

Report

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