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?

  • why don't use tabindex="1" property on your input field. – Rohit Mourya Mar 24 '17 at 6:33
  • @RohitMourya I have tried that, unfortunately it is not working either. – Devendra Mar 24 '17 at 6:41
  • have you tried $document.ready or may be if you could just post your code. – Rohit Mourya Mar 24 '17 at 6:49
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();
    }, 100);  // 0.1 seconds after load time.
);
  • This solution got worked. Thank you @sfdcfox for your help :) – Devendra Mar 24 '17 at 23:12

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.