function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EricVlachEricVlach 

How to remove focus from first input field

By default, the focus is set to the first available inputField. In my case, the first input is a date field. Every time I go to the page, the focus is set to that field and the calendar pops up, covering items below it.

How do I remove the auto-focus? screenshot:

 


Best Answer chosen by Admin (Salesforce Developers) 
CTU007CTU007

In my VF page, the first input field is a checkbox, but the focus on page load is also set to the second input field -- a date field.

 

Any explanation?

All Answers

TehNrdTehNrd
I'd like to know this as well. Unfortunately I fear that placing a different inputField first may be the "solution" but I certainly hope not.
jwetzlerjwetzler
Someone found a solution here.
TehNrdTehNrd
That javascript solution looks about as long as some of my pages themself. :smileytongue:

Here is a semi-related idea:
http://ideas.salesforce.com/article/show/10089079/Tab_order_or_Visualforce_page

The idea says tab order but really its more like tab direction.  Ideally you would be able to set the tabIndex of each input field.
EricVlachEricVlach
Let's read the entire thread! ;-)

putting

<script>function setFocusOnLoad() {}</script>

in my VF cleared up the problem.
TehNrdTehNrd
Whoops. I looked over it to quickly and thought setFocusOnLoad() {}  was calling the larger setFocus() method.

But how is a function with nothing in it preventing focus?
jwetzlerjwetzler
it's probably overwriting a salesforce js function, which means I probably shouldn't be telling you it's a good solution to use :)

so use at your own risk! </broken record>
TehNrdTehNrd
I figured it had to be something tricky like that. Thanks.
MATTYBMEMATTYBME
Why can't you just create a simple text field and hide it? Then in your VF page you can call that inputField first but with a hidden value. Surely that is a lot simpler?
Ron HessRon Hess
the focus routine skips over hidden fields. 

a proper way to do this is to explicitly set the focus on the field you wish, using a body onload event handler.  Just be sure to avoid stepping on the existing body onload.

here is a proper way to avoid stepping on the onload function used by visualforce

Code:
<script>
    function init() {                
        // set focus on your element here 
} var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } init(); } </script>

 

CTU007CTU007

In my VF page, the first input field is a checkbox, but the focus on page load is also set to the second input field -- a date field.

 

Any explanation?

This was selected as the best answer
Pranav MarathePranav Marathe

This trick worked for me too..

 

Thanks.

Tal One1Tal One1

This solves the issue.

Thanks,

EIS DeveloperEIS Developer

<script>function setFocusOnLoad() {}</script>

 

is a perfectly valid solution. If you look at the source of you apex page you'll see:

 

function bodyOnLoad(){setFocusOnLoad(); ... }

 

There is a lot of other code in bodyOnLoad(), we simply want to override the functionality of setFocusOnLoad(). In this case we're doing nothing.

vanessenvanessen

Its work, yes

sfpsfp
Its works. Thank you very much :)
Chandrakanth MalayathiChandrakanth Malayathi

Thank you very much :)

It is working.

Nisar799Nisar799

function setFocusOnLoad() {}  Works :)
Mason StaerkelMason Staerkel
I just want to say laziest admin ever to say the best answer to this question wasn't even an answer but just a re-query into the same issue as is described on request. Shame shame Salesforce, real lazy...
Rival Suheri 2Rival Suheri 2
Worked like a charm using @EricVlach suggestion.
The best chosen answer by Admin doesn't answer the question at all.
Why most of the best chosen by admin in this forum doesn't answer the question?