Keep focus on an input with jQuery

Adam K Dean - Mar 12 '13 - - Dev Community

The most hair-loss-inducing problems usually have the simplest solutions, and such can be said for my latest shortcut to male pattern baldness.

I'm working on my forever project, and part of it requires that an input keep focus all of the time. User experience out of the window, this is the way it has to be, but .blur() is not cancel-able, nor is .focusout(). This means you can't just call .focus() in the .blur() function. You have to trick it.

Set initial focus first:

$("#input").focus();
Enter fullscreen mode Exit fullscreen mode

And then set the re-focus:

$("#input").blur(function() {
    setTimeout(function() { $("#input").focus(); }, 0);
});
Enter fullscreen mode Exit fullscreen mode

Works a treat.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player