There's a question regarding getting and setting of the caret position in IE on this question:
Get caret position in textarea (IE)
It also references an answer for actually getting the caret position here: How to get the start and end points of selection in text area?
There's a healthy amount of code that's specific to dealing with IE, but it will accomplish what you're trying to do. Note that the getter function returns a selection range, just as the setter can be used to set a selection range. In this case, you'd just call it with startOffset
equaling endOffset
:
setSelection(el, startOffset, endOffset);
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionStart);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}