Moving the caret to the end of text in an <input> element
Avishkar Autar · May 18 2014 · Web Technologies
Very simple, and the following will work in all modern browsers.
HTML
<input name="url" type="text" value="http://" />
Javascript
var inputElem = document.getElementsByName("url")[0];
var valLen = inputElem.value.length;
inputElem.selectionStart = valLen;
inputElem.selectionEnd = valLen;
inputElem.focus();
The same technique will work for <textarea> elements as well.