Copy link to clipboard
Copied
I am looking for a way (potentially using JavaScript) to have a portion of text within a field be selected upon entering a field. Below is an example:
My intention is for the document to increment the highest photo number currently in use by 1 (which is usually what the user will want) and place that after the user's initials. The part I need help with is to select the text of the number in case this "prediction" is wrong. This will enable the user to simply begin typing the number they want in the event that they threw away a photo or otherwise skip a number.
I am using the latest version of Acrobat Pro DC. Thank you for reading, and I appreciate any help anyone has to offer!
Copy link to clipboard
Copied
I don't believe that's possible using a script.
Copy link to clipboard
Copied
What I would do instead is populate the field when the user clicks into it with the next number using the On Focus event (maybe checking it's not empty first, as to not overwrite any custom values the user entered before). This will allow the user to change the value if they wanted to, or just tab to the next field if the automatic value is correct.
Copy link to clipboard
Copied
Okay, I see this potentially still doing exactly what I want. For now I have a simple script putting in the user's intials with the On Focus:
if(event.target.value == ""){
event.target.value = this.getField("Camera").value;
}
With this, the text which is input is not selected. Is there a way to have everything selected once the desired info is "typed in"?
Copy link to clipboard
Copied
The only way to select the text of a field is to set focus to it, which you can do like this:
this.getField("FieldName").setFocus();
This would select the full contents of that field.
Copy link to clipboard
Copied
I don't think this would work on the On Focus event, though, as the field already has the focus at that moment...
Copy link to clipboard
Copied
Okay, I need a way to have the text selected once the user enters the field. Let's start there. Is that possible? Can I call a doc level function that then calls setFocus() back to the field or something?
Copy link to clipboard
Copied
I don't think so. It will only work if you set the focus to another field and then back to it, but that would create an endless loop... I think you're over-complicating this. Use the initial method I described, or abandon this idea.
Copy link to clipboard
Copied
Then I can have the initials added to the beginning with an On Blur trigger (hopefully).