• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Focus on first field on open

Community Expert ,
Sep 21, 2019 Sep 21, 2019

Copy link to clipboard

Copied

I have stumbled into a nice on OpenPage javascript that automatically set the focus on the first field to be filled. Nice.

 

if (typeof x == "undefined")
{ 
var x = 1 
var premier = this.getField("T1");
premier.setFocus(); }

 

But why the if statement to check for var x? What does it do exactly? I can get the same results without it by using only teh last two lines.

TOPICS
Acrobat SDK and JavaScript

Views

557

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2019 Sep 22, 2019

Copy link to clipboard

Copied

Hi

 

This "var x" is useless, and undefined should not have quotes since it's a JavaScript keyword.

 

if (this.getField("T1") != undefined) {this.getField("T1").setFocus();}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2019 Sep 22, 2019

Copy link to clipboard

Copied

What purpose does it give to verify if "T1" is undefined ? Using just this is working.

this.getField("T1").setFocus();

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2019 Sep 22, 2019

Copy link to clipboard

Copied

Purpose is to avoid to set focus on a field that doesn't exist, because this will thrown a error.

 

But you can simply use

this.getField("T1").setFocus();

since JavaScript handles this kind of small errors very well.

😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2019 Sep 22, 2019

Copy link to clipboard

Copied

This type of coding is used to only run the block of code within the if statement once. So the first time one opens this page, the field "T1" will receive the focus. The next time that page is navigated to, "T1" will not be focused on. 

 

I usually use this when intializing a form on the first open to run one time only some scripts for filling in dates and setting required starting values.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2019 Sep 22, 2019

Copy link to clipboard

Copied

LATEST
That’s beginning of make sense... Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines