Javascript focuses and doesn't work in FF

Using jQuery, the following does not work in FF, but it is in IE

$(this).focus().select();

I looked around and found that you can use the timeout to get around this, but that’s not what I want to do if I can avoid it. Does anyone know another way to do this and make it work in FF?

Metropolis

+3
source share
4 answers

. , setTimeout() - , Firefox. , : IE , focus(), Firefox , , , . setTimeout() , , , .

+5

,

setTimeout(function() 
 {
   $(Selecter).focus(); 
 }, 0);
+3

$('input').focus().select();

 <input type="text" value="Some text" />

firefox. , , .

0

The solution to this that I just found is to use the code below.

[elementHere].setSelectionRange(0, [elementHere].value.length);

According to the Mozilla Developer Documentation , this selects the text but does not focus it. At least for me, this prevented problems with selecting text inside the focus event handler, because selecting text does not cause the element containing it to be focused again.

0
source

Source: https://habr.com/ru/post/1739507/