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

Second part of if then statement not working

kenwarthen
Registered: Jun 27 2010
Posts: 22
Answered

I have an Acrobat form with a dropdown box (cboFirstDay) that has two options; Sunday or Monday. I'm using the following code to populate the second day text box (txtSecondDay). If I select Sunday the second day text box will display properly the Monday value. If I select Monday in the drop down box, the second day text box still displays the Monday value. I'm very new to javascript, so any help here will be greatly appreciated. — Ken

var FirstDay=this.getField("cboFirstDay").value;
if (FirstDay="Sunday") {
this.getField("txtSecondDay").value="Monday";
} else {
if(FirstDay="Monday") {
this.getField("txtSecondDay").value="Tuesday";}
}

The code is in the Mouse Up event of the drop down box.

My Product Information:
Acrobat Pro Extended 9.0, Windows
Niall
Expert
Registered: Apr 26 2006
Posts: 62
Since you are only testing against two conditions (Sunday or Monday) you can simplify the script:

var FirstDay=this.getField("cboFirstDay");if (FirstDay.value=="Sunday") {this.getField("txtSecondDay").value="Monday";} else {this.getField("txtSecondDay").value="Tuesday";}

Also included the .value in the if statement.

Hope that helps,

Niall

ps Also the test in the if statement needs a double equals sign "=="

Hope this helps,

Niall
Assure Dynamics

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You might try setting the drop down, list box, or combo box to commit immediately and use the 'custom keystroke' stroke. Since you are focused in the field, you need to use the 'event' object and because you are accessing the keystroke, use the 'changeEx' property.

George Kaiser

kenwarthen
Registered: Jun 27 2010
Posts: 22
I put the following script in the custom keystroke script event

var FirstDay=this.getField("cboFirstDay");
if (FirstDay.value=="Sunday") {
this.getField("txtSecondDay").value="Monday";
} else {
if (FirstDay.value=="Monday") {
this.getField("txtSecondDay").value="Tuesday";
}}

Now when either Sunday or Monday are selected the value in txtSecondDay is Tuesday. However, if I manually delete the value in the txtSecondDay field and then select a value from the drop down box it populates the incorrect value. If I repeat that procedure, it populates correctly on the second attempt. Something is clearly not right here, but again, as a newbie, I'm pretty confused as to what's going on.
Niall
Expert
Registered: Apr 26 2006
Posts: 62
You are Wrapping the second if statement inside the else statement. You could simplify this:
var FirstDay=this.getField("cboFirstDay");
if (FirstDay.value=="Sunday") {
this.getField("txtSecondDay").value="Monday";
} else if (FirstDay.value=="Monday") {
this.getField("txtSecondDay").value="Tuesday";
}

However I would take out the else if and just run with the shortened code above.

Hope this helps,

Niall
Assure Dynamics

kenwarthen
Registered: Jun 27 2010
Posts: 22
Okay. I used your first script. I still have to manually delete the value in the txtSecondDay field before it will work properly. If I don't the value does not change. And again, it only works correctly if I repeat the manual deletion of the second day value a second time.
Niall
Expert
Registered: Apr 26 2006
Posts: 62
Is the script in the dropdown?

It sounds like it is in the second field, which would not be correct for the commit event. Either use the calculate event of the second field or the commit of the dropdown.

Hope this helps,

Niall
Assure Dynamics

kenwarthen
Registered: Jun 27 2010
Posts: 22
The script is in the drop down. I have the drop down Commit selected value immediately option checked and the script in the Custom Keystroke Script field (though I previously had the script in the Mouse Up event which didn't work either).
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
See you post in Planet PDF

[url=http://forum.planetpdf.com/wb/default.asp?action=9&fid=34&read=63707&Msg=Show]populating text fields based on dropdown selection[/url] for a working example.The custom keystroke script:
if(event.changeEx == 'Sunday') {this.getField('cDayOfWeek.0').value = 'Monday';this.getField('cDayOfWeek.1').value = 'Tuesday';this.getField('cDayOfWeek.2').value = 'Wednesday';this.getField('cDayOfWeek.3').value = 'Thursday';this.getField('cDayOfWeek.4').value = 'Friday';this.getField('cDayOfWeek.5').value = 'Saturday';}if(event.changeEx == 'Monday') {this.getField('cDayOfWeek.0').value = 'Tuesday';this.getField('cDayOfWeek.1').value = 'Wednesday';this.getField('cDayOfWeek.2').value = 'Thursday';this.getField('cDayOfWeek.3').value = 'Friday';this.getField('cDayOfWeek.4').value = 'Saturday';this.getField('cDayOfWeek.5').value = 'Sunday';}

George Kaiser

kenwarthen
Registered: Jun 27 2010
Posts: 22
Thanks for all the help.

I finally got it to work using the code below in the Custom Key Stroke event. Any recommendations for beginners texts specific to Acrobat forms javascript would be a big help.

Ken


var FirstDay=this.getField("cboFirstDay")
var field2=this.getField("txtSecondDay")
var field3=this.getField("txtThirdDay")
var field4=this.getField("txtFourthDay")
var field5=this.getField("txtFifthDay")
var field6=this.getField("txtSixthDay")
var field7=this.getField("txtSeventhDay")
if(event.value == "Sunday")
{
field2.value="Monday";
field3.value="Tuesday";
field4.value="Wednesday";
field5.value="Thursday";
field6.value="Friday";
field7.value="Saturday";
}
else
{
field2.value="Tuesday"
field3.value="Wednesday";
field4.value="Thursday";
field5.value="Friday";
field6.value="Saturday";
field7.value="Sunday";
}
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi kenwarthen,

Acrobat JavaScript is based on Core JavaScript and the best text specifically on Core JavaScript language would be "JavaScript The Defintive Guide by Dave OFlanagan ( Oreilly publishers). This text will provide you with all the basic syntax and a good grounding. For Acrobat specific JavaScript there aren't really any good textbooks on it. There are some good Acrobat and PDF books that provide some scripting coverage such as the PDF forms Bible by Ted Padova and Angie Akomoto (Wiley publishers).

And of course, you may want to read one of the 70 or so tutorials in the Learning Center here on a huge variety of scripting topics for Acrobat and LiveCycle Designer PDF forms.

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com