These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Adobe javascript syntax

steveald
Registered: Jun 29 2010
Posts: 15
Answered

I'm sure I just need to learn Adobe javascript basics. But, perhaps if someone could apply the appropriate syntax to the following if/then calculation, it would go a long way toward helping me grasp the basic concept. (I had to strip out all formatting I had managed to figure out in order to get this message to post.)

This is for a Custom Calculation Script where data entered into another Text Field (called "YearsWithCarrier") controls what appears in the field this calculation is defined in - lets call it Percentage. Depending on what is entered into YearsWithCarrier, Percentage displays either 10, 12.5 or 15. This result will then be used to calculate the value that appears in yet another field.

I know I need to define a variable and the value property. And the javascript formatting needs to be added back into the formula. Thank you for your assistance.

If YearsWithCarrier is less than 3, then enter "10". Else, if YearsWithCarrier is greater than 4 then enter "15". Else, enter "12.5".

My Product Information:
Acrobat Pro 8.1.6, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
YearsWithCarrier = this.getField("YearsWithCarrier").value;if (YearsWithCarrier<3) {event.value = "10";} else if (YearsWithCarrier>4) {event.value = "15";} else event.value = "12.5";

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

steveald
Registered: Jun 29 2010
Posts: 15
Well, you sure made that appear simple.

Thank you!
steveald
Registered: Jun 29 2010
Posts: 15
If you don't mind a little elaboration:

It appears that the YearsWithCarrier inside the quotes and parentheses must be named exactly so in order to match the field the script is getting data from, while the YearsWithCarrier at the beginning of the first line and in the ensuing parentheses sets could be called anything since it is defined and used only within the javascript - "Bob" could be used instead for instance.

I assume the protocol of having no . or / characters and no spaces in the defined names applies here as well.

And it appears the .value, event.value terms and all the punctuation are just proper syntax for the javascript to work correctly.

Does that sound about right?

Thanks, again. The javascript works perfectly, by the way.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
steveald wrote:
If you don't mind a little elaboration:It appears that the YearsWithCarrier inside the quotes and parentheses must be named exactly so in order to match the field the script is getting data from, while the YearsWithCarrier at the beginning of the first line and in the ensuing parentheses sets could be called anything since it is defined and used only within the javascript - "Bob" could be used instead for instance.
Correct.

Quote:
I assume the protocol of having no . or / characters and no spaces in the defined names applies here as well.
No, using this method you can call your field whatever you want, as long as you use exactly the same name between quotes. However, if you use special characters like slashes, you will need to "escape" them, ie. to add a backwards-slash before them. So if the field is called "Field1/2", you will need to access it like so:
this.getField("Field1\/2")
However, I wouldn't recommend doing that.

Quote:
And it appears the .value, event.value terms and all the punctuation are just proper syntax for the javascript to work correctly.
Correct. You should really have a look at the Acrobat JavaScript Reference files for more information about how it works if you want to create your own scripts.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com