1

I'm trying to add an input box that is always in focus, so even if you click somewhere else on the website and start typing, it will still type in that box. I found a solution that works with Google Chrome, which is to add this attribute to the input element: onblur="this.focus()", it will focus the element whenever the element is unfocused.

But it didn't work with Firefox, so I found someone saying that this works:

$('#command').on('blur', function () {
    $('#command').focus();
});

But that didn't work either. The 'blur' event fires, but focus() won't work. I found out that the 'onblur' event works, and the part that causes the problem is the function focus() itself. So are there any alternative ways to make an input box focused?

Here is an example:

<input autofocus onblur="this.focus()" placeholder="This is focused.">
<input placeholder="Can't focus on this.">
Works with Chrome, but not Firefox. The input box won't even be autofocused?
Marked as Duplicate
  • 1,117
  • 4
  • 17
  • 39

1 Answers1

0

Try maybe forcing a click on the input.

$('#command').trigger('click');

EDIT

Surprisingly forcing a click wont work, and also adding a label hidden and forcing a click on label bound to the input wont work either;

you may not like this but with a setTimeout works..

setTimeout(function() {$('#command').focus();},1)
Sonicd300
  • 1,950
  • 1
  • 16
  • 22