javascript - How to set cursor position at the end of input text in Google Chrome -


This is my first question, therefore, if I do something wrong then I already have to forgive.

With the Focus function I am trying to set the cursor in the text field. I did some research but none of the solutions provided in Google Chrome works properly in Internet Explorer and Firefox:

JS:

  $ ('# Search'). Focus (function () {var SearchInput = $ ('#Search'); var Strangelong = SearchInput.val (). Length; Search input.focus (); SearchInput [0] .SettingsRangle (Stellenath, Strellanth);});  

html:

  & lt; Input type = "text" id = "search" value = "hello world" />  

Here's the link for me.

Is there any way to do this work in Google Chrome? Thanks a lot!

It seems that the focus event is placed before the cursor when you focus input, one Hackney Workaround A settimeout will be used like this:

  $ ('#search'). Focus (function () {setTimeout ((function (L) {var strLength = el .value.length; return function () (if (el.setSelectionRange! == undefined) {el.setSelectionRange (strLength, strLength);} and {$ (L) .val (el.value);}}}}} (this)), 0);});  

Try this Bela:

The 0ms timeout was edited, as @SPRK reported that it is sufficient to reach the end of the execution queue.


Comments