Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

[AEM Gems Webinar] Accelerating Experience Manager as a Cloud Service development with Rapid Development Environments
Adobe Summit 2023 | Complete list of AEM Sessions & Labs
SOLVED

How do I set focus after initialization?

Avatar

Level 7

Problem: I'm having trouble setting the focus to a new text field after it is initialized.

Background: I have created a form with a table that creates a new row after leaving the last textfield/cell in the last row if it is needed. Once I tab out of the last field, focus is lost completely--i.e., tabbing does not reach any other parts of the form.

Attempts: I have attempted to set focus on the first text field in the new row by using (rowNum is a variable for the newly created row)

xfa.host.setFocus("xfa[0].form[0].form1[0].#subform[0].Subform2[0].Table1[0].Row1[rowNum].TextField1[0]");

and

this.parent.parent.Row1[rowNum].TextField1.setFocus();

I have also tried to set focus back to the last field I had just exited with

xfa.host.setFocus("this");

I have also tried using the action builder to bring focus to the newly created textfield when it is initialized.

[edit] I forgot to say that the attempt to set the focus back to the last field I had just exited actually worked. [/edit]

Message was edited by: jasotastic81

1 Accepted Solution

Avatar

Correct answer by
Level 4

Looks like you used the variable as a literal string... you need to concatenate the value of the variable into the SOM expression:

xfa.host.setFocus("xfa[0].form[0].form1[0].#subform[0].Subform2[0].Tab le1[0].Row1[" + rowNum + "].TextField1[0]");

Note the two + around the variable name, to join it's value with the rest of the string.

Let us know if this helps.

-Scott

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

Looks like you used the variable as a literal string... you need to concatenate the value of the variable into the SOM expression:

xfa.host.setFocus("xfa[0].form[0].form1[0].#subform[0].Subform2[0].Tab le1[0].Row1[" + rowNum + "].TextField1[0]");

Note the two + around the variable name, to join it's value with the rest of the string.

Let us know if this helps.

-Scott

Avatar

Level 7

@past-tense

It didn't fix it exactly, but it did put me on the right track. I think I may have changed some of the containers after posting my question while working on other parts of the form.

Here was my solution:

var s = "this.parent.parent.Row1["+rowNum+"].TextField1";

xfa.host.setFocus(s);

I had not even considered that I was using a literal string, despite the quotation marks...

Avatar

Level 4

Hey again, glad you worked it out.

Just for clarification, a literal string is anything between quotes. Literal strings are just plain text. Including the name of the variable in the string does not actually cause a reference to the value to exist. The compiler just sees text, and doesn't know which (if any) parts of the text you intended to reference a variable.

So when you wrote:

xfa.host.setFocus("xfa[0].form[0].form1[0].#subform[0].Subform2[0].Table1[0].Row1[rowNum].TextField1[0]");

if was almost like trying to reference a field directly by using:

xfa.form.form1.subform.subform2.table1.Row1["rowNum"].TextField1

"rowNum" is just a string, one that 'just so happens' to have the name of your variable in it. The compiler has no clue what to do with a string as an index, so it fails.

Hope that helped to explain it a bit better.