Skip to main content
jctremblay
Community Expert
Community Expert
September 22, 2019
Question

Focus on first field on open

  • September 22, 2019
  • 3 replies
  • 772 views

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.

This topic has been closed for replies.

3 replies

Inspiring
September 22, 2019

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.

 

 

jctremblay
Community Expert
Community Expert
September 22, 2019
That’s beginning of make sense... Thanks!
JR Boulay
Community Expert
Community Expert
September 22, 2019

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.

😉

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
September 22, 2019

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();}

 

Acrobate du PDF, InDesigner et Photoshopographe
jctremblay
Community Expert
Community Expert
September 22, 2019

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

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