I'm looking for a way to set a selection in a textarea in Internet Explorer. In other browsers, this works just fine:
textarea.selectionStart = start;
textarea.selectionEnd = end;
In IE, I assume I have to use createRange
and adjust the selection somehow, but I cannot figure out how.
Extra bonus points for a link to a proper documentation about createRange
and associated methods, MSDN isn't helping out much.
This works for me:
Useful links:
moveStart() at MSDN: http://msdn.microsoft.com/en-us/library/ms536623%28VS.85%29.aspx
moveEnd() at MSDN: http://msdn.microsoft.com/en-us/library/ms536620%28VS.85%29.aspx
Try with
Beware of line separators, move* methods see them as one character, but they are actually two - \r\n
As already commented the move methods see the line separators as one character (
\n
) instead of two (\r\n
). I have adjusted the routine to compensate for that: