Search This Blog

Friday, 17 May 2013

On click select all text in a form field

Problem

In some scenarios a form user may need to go through a form replacing values in the fields. If the user has a preference for using the keyboard and tabbing to the field then this is easy as the contents of the field is selected, when the user starts typing they overwrite the value. 
If your users have a preference for using the mouse then the behaviour is a bit different. When the user clicks into a field they go into insert mode at the point where they click. To replace the contents of the fields they can delete all the characters and start typing, or press Ctrl-A (to select the fields contents) and then start typing, or select the contents of the field with the mouse and start typing. In this situation it would be easier for the user if clicking into a field selected the contents.

Solution

The solution here was to add some code to the click event of the fields to replicate the behaviour of the tabbing though the form.

Detailed explanation

The code in the click event is;

xfa.host.setFocus(SelectOnClick.somExpression);
app.setTimeOut("xfa.host.setFocus('"+this.somExpression+"');",50);

This code sets the focus to the field SelectOnClick then 50 milliseconds later sets the focus back to the current field, setting focus this way causes the contents of the field to be selected, just like tabbing to it.

For this to work the field SelectOnClick must be set to Visible (Print Only), which means it can still receive focus but is not visible to the user. Then in the prePrint event set the field to hidden and in the postPrint event set the field to visible so the user won't see it on paper either.

This sample, OnClickSelect.pdf, shows a normal field, a field with the above click event code and a field that selects all text on a double click.


5 comments:

  1. Hi. I'm hoping to eventually combine this "select all" script with your script for double clicking a cell. If the user double clicks on the cell, all of that cell's contents would be selected. If there is only click, the cursor inserts at that position in the cell (its default behavior).
    I'm having trouble getting this "select all" script to work by itself, however. Would be able to post your sample pdf?
    Thanks!
    Raymond

    ReplyDelete
  2. Hi Raymond,

    I've attached my sample to the post and added a field with the double click behaviour you described. I hope it helps.

    Bruce

    ReplyDelete
    Replies
    1. Thanks so much for posting your file, especially with the double click already done!
      I have a couple of followup questions for you, because after trying to place it into my form, it still doesn't work.

      1) Will the single click or the double click work if the field has multiple lines? [Most of the fields I'll use the double click for will have multiple lines.]
      Since the scripts didn't work on my form, I adjusted your form by setting "Expand to fit" (height) and "Allow Multiple Lines" for TextField2 and TextField3. I was surprised that both the single and the double click stopped working on your form with those settings.

      2) On my form, I'll use the double click in a field in rows on 2 of my tables. The tables are in separate subforms. Will I need to change the reference to the SelectOnClick field (assuming I put SelectOnClick higher in the ContentPage hierarchy) within the scripts?

      Thanks again for the help!
      Raymond

      Delete
    2. Hi Raymond,

      I was not expecting that behaviour. It seems the double click code is working, but fields with allow multiple lines don't seem to work the same way when tabbing either. They always go into insert mode at the end of the value. I can't think of a why to resolve that.

      You will need to change the reference to the SelectOnClick field, you also might want to have multiple SelectOnClick fields if you want to keep your table self contained.

      Good luck

      Bruce

      Delete
    3. Bruce,
      Thanks for checking into it. It seems an odd limitation by Adobe, but I must be the only person who is interested in it.
      I've just asked about it on the Adobe forum just in case, but if you don't know of a way, I'm sure it can't be done.

      Thanks again!

      Raymond

      Delete