Conditional Statements for Acrobat JavaScript
if/else statements are very useful in PDF fillable forms
Conditional JavaScript statements execute actions if those conditions are met. A simple if statement contains the word if, followed by a boolean statement inside brackets, followed by the script to be excecuted if the statement is true. A boolean statement returns either true or false. The script to be executed can be encased inside curly brackets { } but it is not necessary. I always use curly brackets because it makes it easier for me to see exactly which part of a code belongs to the condition.
Boolean Examples
All of the following statements return either true or false when you run them in the console (descriptions in brackets):
3==4; //(3 equals 4) false
3!=4; //(3 does not equal 4) true
3>4; //(3 is greater than 4) false
3>=4; //(3 is greater than or equal to 4) false
3<4; //(3 is less than 4) true
3<=4; //(3 is less than or equal to) true
3*4==12;// (3 x 4 equals 12) true
The above examples are all math statements, but they don’t have to be:
"Cat"=="Cat"; //("Cat" equals "Cat") true
"Cat"=="cat"; //("Cat" equals "cat") false
this.getField("myField").value=="Cat"; (The value of the field named "myField" equals "Cat")
this.getField("myField").borderStyle=="solid"; (The border style of the field named "myField" is "solid")
If Statement Examples
if(3==3)
{app.alert("Correct!")}
if(3==4)
{app.alert("Correct!")}
If the statement is true, an alert that says "Correct!" will pop up. Therefore, it will only pop up in the first example.
if/else Statements
With an if/else statement, if the statement is true, a script will execute as in the previous example. The script to execute is followed by the word else and another script. If a statement is not true, another script will execute. For example:
if(3==3)
{app.alert("Correct!")}
else
{app.alert("Incorrect!")}
In the example above, an alert that reads "Correct!" will pop up. In the following example, an alert that reads "Incorrect!" will pop up:
if(3==4)
{app.alert("Correct!")}
else
{app.alert("Incorrect!")}
There’s another way to write these types of statements that goes like this:
boolean statement - question mark - script if statement true - colon - script if statement false
An example of the previous script written with the style above:
3==4?app.alert("Correct!"):app.alert("Incorrect!");
The script above will pop "Incorrect!" because 3==4 is false. The following script will pop "Correct!":
3==3?app.alert("Correct!"):app.alert("Incorrect!");
I developed an online Acrobat JavaScript course that walks you through different topics with real examples using interactive PDFs, and you can work at your own pace. You can view the course outline here and get full details about the course.
These statements can have multiple conditions and corresponding scripts prior to the final else statement, by seperating the conditions by the words else if (If statement A is true, do this. If statement A is false but statement B is true do this. If statement B is false but statement C is true do this. If all of the previous statements are false, do this.):
if(1>3)
{app.alert("1 is greater than 3.")}
else if
(2>3)
{app.alert("2 is greater than 3.")}
else if
(3>3)
{app.alert("3 is greater than 3.")}
else
{app.alert("All of the previous statements are false.")}
Running the previous code in the console will pop an alert that says "All of the previous statements are false." As soon as one of the statements is true, the code related to it runs and then the script stops. The else if only applies if the previous statement returned false. If you change the code slightly it will pop a "4 is greater than 3" alert:
if(1>3)
{app.alert("1 is greater than 3.")}
else if
(4>3)
{app.alert("4 is greater than 3.")}
else if
(3>3)
{app.alert("3 is greater than 3.")}
else
{app.alert("All of the previous statements are false.")}