Skip to content

Instantly share code, notes, and snippets.

@daihuaye
Last active August 29, 2015 14:07
Show Gist options
  • Save daihuaye/172192965b4246804a24 to your computer and use it in GitHub Desktop.
Save daihuaye/172192965b4246804a24 to your computer and use it in GitHub Desktop.
Move cursor to position in input text box
function setCursorPosition(e, pos) {
$(e).each(function(index, elem) {
if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
var range = elem.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