rybo111 rybo111 - 8 months ago 85
HTML Question

jQuery focus not working in Chrome

Please see this fiddle: http://jsfiddle.net/yg49k/

The following code works fine in FireFox but doesn't work in the latest version of Chrome.


<input type="text" id="one" placeholder="Type two chars" />
<input type="text" id="two" placeholder="It should focus here" />


$("#one").on("input", function() {
if($("#one").val().length == 2) { $("#two").focus(); }

Does anyone know how I can get around this?


Seems like a bug in Chrome. Sometimes it is too fast to execute events properly;)

Found a workaround http://jsfiddle.net/Rd2rZ/

$("#one").on("input", function() {
    if($("#one").val().length == 2) { 
        }, 1);

Use setTimeout with a minimal delay. It will slow down Chrome and make it work.