View Full Version : caret positions

03-21-2005, 12:48 PM
is there any way to get the caret's character and line positions (preferably start and end of each) when selecting text in a textarea?

i have looked at

but the closest i can find is a pixel number
objTextArea.caretPos = document.selection.createRange().duplicate();this just runs a selection.. heres the sort of thing im looking for:
This is some
sample textif you selected "ome \nsamp" it would return something like "characters 16 to 25, lines 2 to 3"
(im not sure about that 16 and 25, that includes a crlf as a character)
a way of moving the caret to the start of a line specified like ctrl+g does would be nice too but this isnt really what im asking for :rolleyes:

03-21-2005, 01:35 PM
I'm not sure about getting coordinates in a textarea, but calling the select() method on a range object (previously set onbeforeeditfocus) will result in commands being sent to the caret point/range, in IE.

03-22-2005, 02:33 PM
i cant find any documentation on how to use it, will it only include things that are typed after the select is made or will it work for the whole thing

if i can get character number i can get line number easily enough from that but i cant get either

03-22-2005, 02:56 PM
Yeah it works either on a selection, if there is one, or from the point of the caret, if not--but I've only tested it w/ a rich-text (contenteditable) control.

The TextRectangle Object (http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/textrectangle.asp) might be useful...