2

I am trying different code suggestions to change the focus to another field instead of text field using jQuery:

j$('.elementId').focus(); 

and changing the focus on load:

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

But none of them seem to work. The focus goes to the field which I am trying to set. but after a second it then changes over to the text field.

I have tried using window.onload, and it works too. But it breaks the dependent picklist on the page. (As depedent picklist is not getting populated load when we use window.onload)

Is there any other way to achieve this?

3
  • why don't use tabindex="1" property on your input field. Commented Mar 24, 2017 at 6:33
  • @RohitMourya I have tried that, unfortunately it is not working either.
    – Devendra
    Commented Mar 24, 2017 at 6:41
  • have you tried $document.ready or may be if you could just post your code. Commented Mar 24, 2017 at 6:49

1 Answer 1

3

The standard library automatically selects the first field for you. The only way to guarantee that you will "beat" the built-in logic is to use asynchronous code:

window.addEventListener("load", function() {
    setTimeout(function() {
        document.querySelector(".elementId").focus(); //do something
    }, 100);  // 0.1 seconds after load time.
});
1
  • This solution got worked. Thank you @sfdcfox for your help :)
    – Devendra
    Commented Mar 24, 2017 at 23:12

You must log in to answer this question.

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