+ Start a Discussion
Mikko4Mikko4 

Blur inputField on page load

Hi!

 

You know that first inputField on the page gets focus on page load? So when the first inputField is a date field then the date picker annoyingly pops up automatically. Does anyone know a way to prevent this?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower
<script type="text/javascript">	
// Note: this is a total hack into Salesforce's "main.js" script for "setFocusOnLoad which is: // function setFocusOnLoad() { // beenFocused || setFocus(); // } // So, if we tell it that the focus has already been set, it does a shortcircuit evaluation and doesn't // run the Salesforce code to set the focus itself. e.g. the Date Pickers don't come up. beenFocused = true;
</script> 

 

Add that to the bottom of your page in question before the </apex:page> tag and it will prevent Salesforce from giving the DatePicker the focus.  (Of course it doesn't leave the focus anywhere useful, so you could fix that yourself if you wanted to... I typically leave it hanging and let the user decide where to start.)

 

This is, of course, a total hack which could break if Salesforce were to change this code... a more canonical way would be to select one of your elements which isn't the datepicker and set the focus to that instead.  But this works well and the datepicker never gets the focus in the first place.

 

I suspect someone may have a more proper solution out there on the boards somewhere...

 

Best, Steve.

All Answers

SteveBowerSteveBower
<script type="text/javascript">	
// Note: this is a total hack into Salesforce's "main.js" script for "setFocusOnLoad which is: // function setFocusOnLoad() { // beenFocused || setFocus(); // } // So, if we tell it that the focus has already been set, it does a shortcircuit evaluation and doesn't // run the Salesforce code to set the focus itself. e.g. the Date Pickers don't come up. beenFocused = true;
</script> 

 

Add that to the bottom of your page in question before the </apex:page> tag and it will prevent Salesforce from giving the DatePicker the focus.  (Of course it doesn't leave the focus anywhere useful, so you could fix that yourself if you wanted to... I typically leave it hanging and let the user decide where to start.)

 

This is, of course, a total hack which could break if Salesforce were to change this code... a more canonical way would be to select one of your elements which isn't the datepicker and set the focus to that instead.  But this works well and the datepicker never gets the focus in the first place.

 

I suspect someone may have a more proper solution out there on the boards somewhere...

 

Best, Steve.

This was selected as the best answer
Mikko4Mikko4

That's perfect! Thanks Steve!

 

My page only has this one date field and then a dataTable so this solution works well for me.