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

If Then Java Script

Andrew_WilsonTN
Registered: Jul 28 2010
Posts: 1
Answered

I am trying to get an application to pull a price based on the particular product they select. The code I have written is

var v1 = Combo Box1;
var f2 = getField("Rating");

if (v1 = "Safety/Supervisor Freediver") f2.value = "267.50";
if (v1 = "Asst Freediver Instructor") f2.value="370.50";
if (v1 = "Basic Freediver Instructor") f2.value = "679.50";
if (v1 = "Intermediate Freediver Instructor") f2.value = "782.50";
if (v1 = "Advanced Freediver Istructor")
f2.value = "885.50"
if (v1 = "Instructor Trainer")
f2.value = "885.50"
if(v1 = "Instrutor Trainer Trainer")
f2.value = "885.50"

I am brand new to writing Java Script and the following error comes up when I try to save:

missing; before statement
1: at line 2

Any help would be greatly appreciated.

My Product Information:
Acrobat Pro 9.3.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
If you are trying to create a variable with a character string value, the string value needs to be delimited by matched quota ion marks. If the text is not within quotation marks, JavaScript assumes what you have entered is a variable or garbage if a variable has not been defined.

You should look at the various tutorials and eSeminars on demand to learn how write Acrobat JavaScript.

George Kaiser

iakovq
Registered: Feb 1 2007
Posts: 8
A cleaner script would be something like,

switch (v1 or whatever you're testing for)
{ case "string1":
f2.value = something1; break;
case "string2":
f2.value = something2; break;
...
...
}

Take this opportunity to learn about the 'switch' statement.

Sincerely,
pleht dot com
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
The custom calculation script could be using the switch statement:
// name of fieldvar field1 = "Combo Box1";// get the fieldvar v1 = this.getField(field1);// make selection on value of the fieldswtich(v1.value) {case  "Safety/Supervisor Freediver"):event.value = "267.50";break;case "Asst Freediver Instructor":event.value="370.50";break;case "Basic Freediver Instructor":event.value = "679.50";break;case "Intermediate Freediver Instructor":event.value = "782.50";break;// multiple values with same actioncase "Advanced Freediver Instructor":case "Instructor Trainer":case "Instructor Trainer Trainer":event.value = "885.50"break;// all other valuesdefault:event.value = '';break;}  // end switch

The above method allows for setting a devault value if there is no match and easily handles multiple evaluation values that have the same result.

George Kaiser