I've tried using other code suggestions to change the focus to another field:


and changing the focus on load:

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

But neither of them seem to work. I notice the focus is on the field I want it on for a brief second but then changes over to the date field. Is there a specific place I should put this code to change the focus?

  • What are you using for your date field. Is it a standard salesforce date field or are you using some sort of calendar / date picker library. the setFocusOnLoad should work without fail unless there is some script in a library etc overriding it.
    – Eric
    May 3, 2015 at 17:40

3 Answers 3


Give this a try. I have similar code working but I blur all the text fields and set focus to a list of records at the top of my page.

<script src="http://code.jquery.com/jquery-2.0.0.min.js" />
    window.onload = function(){
  • You really shouldn't use window.onload, though. Especially not with jQuery. This code is irresponsible at best.
    – sfdcfox
    May 2, 2015 at 11:57
  • This seems to be the only solution that has worked. Before I mark it as answered, @sfdcfox why would it be bad to use jQuery with the window.onload event?
    – Cameron
    May 4, 2015 at 15:53
  • @Cameron window.onload doesn't allow multiple functions, so if something else uses window.onload, last one to load wins, doubly so if you have to shim for IE 8. In fact, salesforce has an onload function on the body already... You could break something! Instead, you should always use jQuery.ready, as in $(function() ... { ... }); Also, only one element can be in focus at once-- so why would you blur the prior element first?
    – sfdcfox
    May 4, 2015 at 20:19

For a purely JS solution with no need to load libraries (jquery etc)

All you have to do is add this to the vf page:


   function setFocusOnLoad(){}


and you will be all set

  • I thought this was the solution as well but OP claims to have already tried this.
    – Adrian Larson
    May 2, 2015 at 13:01
  • @AdrianLarson - Hmmmm. I did not even see that lol. I just replied with a well known working solution thinking it was a simple answer. Thats what I get for assuming....
    – Eric
    May 3, 2015 at 17:41

Ideally, you want to queue up your focus command to just after onload. This avoids any race conditions and interactions with the system library.

This little snippet does just that:

        function() {
        }, 4);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .