Last active
August 29, 2015 14:07
-
-
Save daihuaye/172192965b4246804a24 to your computer and use it in GitHub Desktop.
Move cursor to position in input text box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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