2

This code can work on IE, but can't work in input on chrome,

 onkeyup="javscript:if(event.keyCode ==13){frm1.identifyCode.focus();}"

I tried to set timeout but still doesn't work.

setTimeout(function(){frm1.identifyCode.focus();},0);
Vandal
  • 903
  • 1
  • 14
  • 27
Louis
  • 123
  • 1
  • 3
  • 11
  • You realize you have `javscript`, not `javascript`, in your code? I'm pretty sure it's just a transcription error, because otherwise, it shouldn't work in IE either, but you might want to check anyway. – Martha Oct 13 '15 at 14:47

5 Answers5

2

Try changing your timeout to 1 it might fixed the issue . It will have a minimal delay and slow down execution in chrome. I think this is a bug on chrome.

setTimeout(function(){frm1.identifyCode.focus();},1);

similar question jQuery focus not working in Chrome

Community
  • 1
  • 1
bot
  • 4,841
  • 3
  • 42
  • 67
1

Excel Gamboa is right, the transfer to end functions solved my problem. Example:

jQuery(".search-icon").click(function(){
    var $navbarToggler =  jQuery(".navbar-toggler");
    var $navbarCollapse = jQuery(".navbar-collapse");
    var $mobileSearch =jQuery('.search_mobile_form');
    var $mobileOverlay =  jQuery(".mobile-overlay");
    var $inputSearch = jQuery("#search");
    if( $mobileSearch.is('.show-search') ) {
        $mobileSearch.removeClass('show-search');
        $mobileOverlay.fadeOut();
    }
    else {
        $mobileOverlay.fadeIn("slow");
        $mobileSearch.addClass('show-search');
        $navbarCollapse.removeClass("show").addClass("collapse");
        $navbarToggler.addClass("collapsed");
        $inputSearch.focus();

    }
    return false;
});
Leo Moshko
  • 19
  • 2
0

It is possible that you are making a call to an instruction or function after calling .focus() that is causing the field to be out of focus. Try to change the position of the .focus() instruction to the end of a function.

Note: If you are using ajax (just in case), put the focus instruction at the end of a success call.

Exel Gamboa
  • 936
  • 1
  • 14
  • 25
0

For what it's worth; I had a similar problem with a scrollable DIV that was not being scrolled by keyboard, unless I clicked in it first.

jQuery's focus() did the trick in Firefox, but Chrome was having none of it. After a good time trying every trick I found online, I eventually solved it by setting tabindex="0" on the element I wanted to focus on, which instantly made it focus-able for Chrome, solving my issue!

Make sure you set it to 0, and nothing else. See the MDN page for reference.

tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is defined by the document's source order.

Klaas Leussink
  • 2,208
  • 16
  • 23
0

Set focus to the element,the browser must first be set to focus.

example:

browser_1.focus();
browser_1.document.getElementById('text_box').focus();
desertnaut
  • 57,590
  • 26
  • 140
  • 166