jQuery focus() sometimes not working in IE8

Solution 1:

I had a similar problem with my app, but I can't reproduce the focus problem with your code. My problem was slightly different in that my page had a link hash that made IE not give my element focus.

Anyway, to get around the problem I added a timeout:

setTimeout(function () {
  $('.my-thing').focus();
}, 100);

Not noticeable by a user but it gives IE a moment to breathe.

Solution 2:

In conjunction with Kazys's solution, I found this to fix all my problems (using IE8 & .HTA files):

$("elem").blur();
$("elem").focus().focus();

I have no idea why, but somehow calling focus twice helps IE along.

EDIT: I have found that calling .show() and .select() can also help.

Solution 3:

Strangely i had the same problem and resolved it using plain old javascript like so:

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

Using jQuery equivalent $('#friend_name').focus(); didn't work in IE8 :-/

Solution 4:

Had similar problem with IE8. I wanted to focus input in dialog when it is opened. Used autoOpen = false. Noticed that focus doesn't work only for first focusable element in dialog. Tried setTimeout but it worked only sometimes. Blurring element before focusing helped me.

$('#dialog').find('#name').blur();
$('#dialog').find('#name').focus();