loading...
Cover image for Keep focus on an input with jQuery

Keep focus on an input with jQuery

adamkdean profile image Adam K Dean ・1 min read

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();

And then set the re-focus:

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

Works a treat.

Discussion

markdown guide