Focus on first field on open

Advocate ,
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, PDF forms

Views

166

Likes

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

Focus on first field on open

Advocate ,
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, PDF forms

Views

167

Likes

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
Sep 21, 2019 0
Adobe Community Professional ,
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();}

 

Likes

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
Reply
Loading...
Sep 22, 2019 1
Advocate ,
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();

 

 

 

 

Likes

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
Reply
Loading...
Sep 22, 2019 0
Adobe Community Professional ,
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.

😉

Likes

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
Reply
Loading...
Sep 22, 2019 0
Adobe Community Professional ,
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.

 

 

Likes

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
Reply
Loading...
Sep 22, 2019 1
Advocate ,
Sep 22, 2019

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Sep 22, 2019 0