+ Start a Discussion

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) 

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

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.
Someone found a solution here.
That javascript solution looks about as long as some of my pages themself. :smileytongue:

Here is a semi-related idea:

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.
Let's read the entire thread! ;-)


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

in my VF cleared up the problem.
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?
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>
I figured it had to be something tricky like that. Thanks.
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

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



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..



Tal One1Tal One1

This solves the issue.


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.


Its work, yes

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

Thank you very much :)

It is working.


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?