3
votes

Since upgrading to the latest Chrome version 34.0.1847.116, the focus event does not seem to be firing when calling

myinput.focus()

in Javascript.

This works fine in Chrome 33.0.1750.154.

test.html

<html>
<head><title>Test</title></head>
<body>
<input id="test_box" onfocus="alert('focused');" />
</body>
</html>

From the console:

document.getElementById('test_box').focus();

No alert in Chrome 34.

Any ideas? Is this a bug? Is there a workaround to fire the event?

3
Works for me. I avoided using alert for my test case (source) because alert mucks about with focus. Tested with Chrome 34.0.1847.116 on Windows, I saw both the message saying I was about to focus, and the message from the onfocus attribute. - T.J. Crowder
I have possibly related issue, downgraded to chrome 33 and the issues are gone.. see stackoverflow.com/questions/23100816/… - giosakti

3 Answers

8
votes

The focus event will fire once you unfocus the Chrome Dev Tools.

2
votes

I don't know if this is actually a bug or bug fix for Chrome v34 if v33 doesn't show the same behavior. (it is due to typing the statement in, in the Console).

If you try http://fiddle.jshell.net/aQ5n6/7/show/

and go to the Console, and type in

document.getElementById('test_box').focus();

it won't alert because the focus is still in the Console... but as soon as you click on the content of the HTML doc (such as any empty space), then it will fire the alert. It looks like when the focus is not actually on the input element (when focus is still on the Console), then the event is not fired.

1
votes

My Version of Google Chrome is 34.0.1847.116

working here Fiddle

<input id="test_box" onfocus="alert('focused');" />

Advice: Check your console for error or warning.