JSFiddle - React, Tailwind, and code Playground
by ozzon91
HTML
<input type="text" value="hello" id="hi">
JavaScript
hi.focus();
setTimeout(function() {
setCaretPosition(hi,0);
}, 3000);
function setCaretPosition(ctrl, pos){
if(ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
} else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}