Skip to content

Instantly share code, notes, and snippets.

@Aetet
Created February 7, 2014 04:50
Show Gist options
  • Save Aetet/8857503 to your computer and use it in GitHub Desktop.
Save Aetet/8857503 to your computer and use it in GitHub Desktop.
Javascript caret
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment