Skip to content

Instantly share code, notes, and snippets.

@xu33
Created August 29, 2013 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xu33/6376149 to your computer and use it in GitHub Desktop.
Save xu33/6376149 to your computer and use it in GitHub Desktop.
Set Cursor Position in Text Area
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
setCaretToPos(document.getElementById("YOURINPUT"), 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment