Skip to content

Instantly share code, notes, and snippets.

@dadon
Created September 21, 2011 13:10
Show Gist options
  • Save dadon/1231988 to your computer and use it in GitHub Desktop.
Save dadon/1231988 to your computer and use it in GitHub Desktop.
[JS] Set selection and cursor position in text input
function setInputSelection(input, startPos, endPos) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(startPos, endPos);
} else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', endPos);
range.moveStart('character', startPos);
range.select();
}
}
function setInputPos(input, pos) {
setInputSelection(input, pos, pos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment