I found this on a different question:
setCaretToPos = function(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();
}
};
setCaretToPos(8, 12);
It should select text from a text area between the 8th character and 12th character.
It works in Firefox and Chrome, but in Opera I get the wrong selection. Offset moves two characters behind
What's wrong with it?
It seems it has something to do with new lines:
\n
because the selection is correct if the text doesn't contain new line character.