Skip to main content
Known Participant
June 13, 2022
Answered

Using buttons to fill text field Acrobat DC

  • June 13, 2022
  • 2 replies
  • 2183 views

Hi.

For simplicity of explination. I am trying to create a PDF form to record the outcome of a coin toss for a number of users over a number of iterations.

I have a fields for toss1 through toss 5.

I would like to have a 2 buttons for the result;- one (called heads) which would return a value of 1 to the toss1 field and if the result is tails another button (called tails)which would return a value of 0 to the toss1 field.

Once either choice is selected I would like the cursor to auto tab to the next field (toss2) etc.

I have tried this with radiobuttons but once I select the second outcome the first one changes as well.

Further I would like to be able to have a blank value option. Again I can insert a Third button to the group which has an output value of "Off" but this resets all recorded entries and not just the active textfield.

Can I do this with a mouseup action on a button rather than utilising a radio button?

This topic has been closed for replies.
Correct answer try67

You don't need anything else, if the field names are correct.

Check the JS Console (Ctrl+J) for errors, or share your file for further help.

2 replies

Nesa Nurani
Community Expert
June 13, 2022

On a 'heads' button as 'Mouse UP' you can use:

this.getField("toss1").value = 1;

Change 1 to 0 for tails button.

What would be the point of autotab in situation with two buttons?

Here is a suggestion, you can make 1 button with custom script that will simulate toin coss and write appropriate word (Heads/Tails or 1/0) in toss fields every time it's clicked it will move to next field and reset fields once all fields are filled.

Known Participant
June 13, 2022

Thanks for your reply

The purpose of the autotab is to move from the recorded result in the Toss1 field to the Toss2 field to be ready to accept the value determined by which button is clicked.

It is not really recording a coin toss that was only so you could visualise that one outcome needed to return a value of one but if the outcome was the opposite the value would be 0

try67
Community Expert
June 13, 2022

Since the script has no way of knowing which field has the focus, that step is not necessary.

try67
Community Expert
June 13, 2022

You should use buttons for this, yes. And setting the focus to the next field is not needed. What you need to do is scan the text fields (let's say they are named "toss1", "toss2", etc.), and fill the last one which is blank.

 

You can do it using this code as the Mouse Up event of the "heads" button:

 

for (var i=1; i<=5; i++) {
	if (this.getField("toss"+i).valueAsString=="") {
		this.getField("toss"+i).value = "1";
		break;
	}
}
if (i>5) app.alert("You can not do another toss.",1);

 

Simply adjust "1" in line #3 to "0" for the "tails" button, or to something else (not a blank value, though!) for the "other" button. For example, you can use "-".

Known Participant
June 13, 2022

Thanks I've put that script into the mouseup javascript action for each of the 3 buttons, but the value on click is not being transferred to the toss1, toss2 etc fields. what do I need to set these actions or calculations to be?

try67
try67Correct answer
Community Expert
June 13, 2022

You don't need anything else, if the field names are correct.

Check the JS Console (Ctrl+J) for errors, or share your file for further help.