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

how to add Var by 1 if visable?

rhoggard
Registered: Jul 21 2008
Posts: 8

I am a total Noob with Javascript so I apologize for asking obvious questions. I am using Acrobat 4.0.

I want different form fields to be visible based on a count of how many are visible already.

In a Button field, that makes another field visible, I thought I could put the following in my "Mouse up action" on each field to be counted:

A Show/Hide field action
and a Javascript action of:
x = x+1

And then in a test Text Form Field I put this in the Custom Calculate Action :
event.value = x;

The test Text field will only update if I click in it, and then hit enter and then click somewhere else on the document. I figure I need it to update on its own as X increases with each press of other fields.

How can I get he value of X to update without me clicking in and out of my test field? Or even do I need to for the final phase as described below:

I haven't figured out the finale bit of Show this field if count is => than x, yet, but I see examples of that on the web.

thanks,
randy

My Product Information:
Acrobat Standard 5.x or older, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
A custom calculation script will only get triggered if some other field value changes. Since you're not changing any field values when you click a button, your calculation is not getting triggered. Instead of using a custom calculation script, it would probably be better to use some code in the Mouse Up event of the buttons to perform the calculation and set the field value of your text field.

Calculated fields should be set to read-only, since it does not make sense to allow a user to attempt to edit it.

Since I don't really understand what you're trying to accomplish, it's difficult to suggest a more specific approach. In general, though, you should use JavaScript to show/hide the fields when the buttons are clicked, as opposed to using show/hide field actions. This will give you the control I think you need. What is supposed to happen if the user clicks the button again and the field it controls becomes hidden? You may want to provide more details about how you've set up the fields and what needs to happen.

George
rhoggard
Registered: Jul 21 2008
Posts: 8
Hi George,
Thank you so much for your reply. I will explain what I am trying to do and maybe you or someone can give me more specific help as you indicated.

I am trying to create a Hangman game for my niece. There is a small button Form field for each letter of the alphabet. When the guessing player guesses a wrong letter, the other player presses a form field button representing that letter. When pressed, the mouse up action causes a larger Text Form Field to display, indicating the used letter that is not part of the Hangman word.

There is a small visible button form field, for each letter of the alphabet. When pressed it causes a larger hidden field with a matching letter to become visible.

What I want to happen is form fields with icon images of the hangman to gradually build the hanging man. If one wrong letter is displayed, then the Hangmans head displays, if two wrong letters, then the head and the body display, and on and on until all parts of the hanging man are displayed.

I was hoping I could somehow script some if/then statements that go something like this:

If "dead letter" count => 1, then display form field Head
If "dead letter" count => 2, then display form field Body
etc

But my problem is I don't know how to have a variable count increase by one, each time a formfield button for a letter is pressed, and of course I would want to be able to subtract if a letter was turned back off.

I hope I am explaining myself better this time. My niece lives very far away, and we use Real VNC to play hangman using Microsoft paint as our shared drawing board. I thought An acrobat document with these form fields I described would make for a much cleaner game for us.

thanks,
randy
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Randy,

Much better! With respect to setting up a variable, probably the best approach is to declare your count variable in a document-level JavaScript. To create one, select "Tools > Forms > Document JavaScripts" (or "Tools > JavaScript > Document JavaScripts" in Acrobat 5), enter a name for it (e.g., "init") and click the "Add" button. Acrobat will supply you with a function skeleton of the same name by default, but go ahead and delete it and add something like the following:// Initialize the bad count variable
var bad_count = 0;

You can now close the code window and "JavaScript Functions" window. What you've just done is declare a variable that is accessible by code you place elsewhere in the PDF, whether it is in a field event (e.g., Mouse Up) or a function you've defined in a document-level script. Code in document-level JavaScripts execute when the document first opens, so that variables and functions are available thereafter.

By the way, although incrementing a variable with code like:

// Increment counter by one
bad_counter = bad_counter + 1;

is perfectly valid, the JavaScript way is to use the increment operator:

// Increment counter by one
bad_counter++;

And if you want to decrease by one:

// Decrement counter by one
bad_counter--;

To control the visibility of a field using JavaScript, you can set the field's "display" property:

// Hide the field named button1
getField("button1").display = display.hidden;

and to show it:

// Show the field
getField("button1").display = display.visible;


This should get you started, but check back if you get stuck with something. Good luck!

George
rhoggard
Registered: Jul 21 2008
Posts: 8
Thanks a bunch George. This will definitely get me started in a more appropriate direction. Your redirection makes perfect sense.

I will report back with successes and further questions as needed.

Thanks again,
randy