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

Can I use the change event?

StevenD
Registered: Oct 6 2006
Posts: 368
Answered

I want to set a field up so if the value in the field is changed then something is done otherwise do nothing.

For example: If a user enters NumField.0 and types 2 then tabs out of the field the calculation 2 * 2 is performed and the value is displayed in NumField.1. However if the user comes back to NumField.0 but doesn't change the value and tabs out of the field the value in NumField.1 doesn't change.

Should I use the change event for something like this or can this even be done. It seems like this kind of thing can be done but I just don't know how to do it.

StevenD

My Product Information:
Acrobat Pro 8.1.2, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Are you using Acrobat or LiveCycle Designer to create your form?

George
StevenD
Registered: Oct 6 2006
Posts: 368
Acrobat. I wish I was using LiveCycle Designer because I have used the change event there before.

StevenD

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
For text fields in Acrobat, the is no event named "change". A text field's Validate event will be triggered when the field value is changed. If the value of any field in the form is changed, any fields that are calculated (e.g., have a custom calculation script) have their Calculate event triggered, in the order defined by the field calculation order. So you have two choices. If you want to use the first field's Validate event, the script might look something like:

// Get reference to another fieldvar f2 = getField("NumField.1"); // Get this field's value, as a numbervar n1 = +event.value; // Set other field's valuef2.value = n1 * n1;

If you want to set up the second field as a calculated field instead, its custom calculation script might look something like:

// Get value of NumField.0 as a numbervar n1 = +getField("NumField.0").value; // Set this field's valueevent.value = n1 * n1;

I usually use the first approach since it is a good idea to minimize the number of fields that have a calculation script, for performance and bug-avoidance reasons.

George
StevenD
Registered: Oct 6 2006
Posts: 368
It looks to me like page 327 of the Adobe Acrobat 7.0 Official JavaScript Reference published by Adobe Press says there is a change event. The sample lines of code that is given is,

//Custom Keystroke for text field
event.change = event.change.toUpperCase();

That is all that is given so I can't really tell by that what else can be done with it.

The reference is also located on page 373 in the JavaScript for Acrobat API Reference (April 2007).

StevenD

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
That's referring to the change property of the event object, not a field's change event. The change property is used in a field's Keystroke event as the code shows.

George
StevenD
Registered: Oct 6 2006
Posts: 368
Right. Thanks for explanation George. I appreciate the help and learning.

StevenD

Runolfr
Registered: Oct 1 2008
Posts: 119
Is there an event that will fire when the value of a checkbox changes, then? I was just trying to set the "Mandatory" property of some fields based on whether a checkbox was checked, and it didn't work.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
If the check box's value is interactively changed, you can look at its value in the Mouse Up event. For example:

// Mouse Up JavaScript for a checkbox // At this point this check box's value has changed // If checked, do somethingif (event.target.value !== "Off") { // Set a field's required propertygetField("Text1").required = true; } else { // Unset a field's required propertygetField("Text1").required = false; }

If the check box's value will be set programmatically or by importing data, you will have to use a different approach. For example, you could set up a (hidden) field with a custom calculation script, which will be triggered whenever the check box's value changes, either manually, programatically, or by importing data.

Check boxes and radio buttons really could use a Validate event...

George
alexpapa
Registered: Nov 25 2008
Posts: 68
I have at one stage used setAction to achieve similar results.

var f = this.getField("RESPONSE_CODE");f.setAction("Format", "javascript:checkResponse();");

This calls the function whenever the contents of the field change on a tab-out.