1

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

document.getElementById('{!$Component.fOpportunity}').focus(); 

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 '15 at 17:40
1

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" />
<script>
    window.onload = function(){
        jQuery('input[type=date]').blur();
        jQuery('[id$=fOpportunity]').focus();
    };
</script>
| improve this answer | |
  • You really shouldn't use window.onload, though. Especially not with jQuery. This code is irresponsible at best. – sfdcfox May 2 '15 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 '15 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 '15 at 20:19
0

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:

<script>

   function setFocusOnLoad(){}

</script>

and you will be all set

| improve this answer | |
  • I thought this was the solution as well but OP claims to have already tried this. – Adrian Larson May 2 '15 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 '15 at 17:41
0

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:

<script>
    window.addEventListener(
        function() {
            document.getElementById(
                 "{!$Component...}").focus();
        }, 4);
</script>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.