Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: caret positions

  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts

    caret positions

    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
    http://msdn.microsoft.com/library/de.../duplicate.asp
    and
    http://www.xulplanet.com/references/...eaElement.html

    but the closest i can find is a pixel number
    Code:
    objTextArea.createTextRange;
    objTextArea.caretPos = document.selection.createRange().duplicate();
    this just runs a selection.. heres the sort of thing im looking for:
    Code:
    Hello.
    This is some 
    sample text
    if 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

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    *this message will self destruct in n-seconds*

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 might be useful...
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •