How do I get the caret position in a textarea?

Discussion in 'Javascript' started by torch, Jun 12, 2004.

  1. torch

    torch Guest

    I have trawled the internet for a solution to this one . All I need is the
    caret position as a number. I have seen quite a few text insertion functions
    that do something like this :

    textarea.caretPos = document.selection.createRange().duplicate()

    and then operate on the caretPos object - but all these solutions seem to
    work by creating a selected area in the text and then replacing it - I just
    want the caret position as a number.

    Anyone got any clues?

    Thanks
     
    torch, Jun 12, 2004
    #1
    1. Advertising

  2. torch

    Ivo Guest

    Ivo, Jun 13, 2004
    #2
    1. Advertising

  3. torch

    Ivo Guest

    "Andrew Thompson" wrote
    > Ivo wrote:
    > >

    http://groups.google.com/groups?threadm=407716b1$0$64635$4a441750@ne
    > > ws.wanadoo.nl

    >
    > Also, try this alternate..
    > <http://groups.google.com/groups?th=296d438401e265d5>
    >
    > (shrugs) I have been practising the 'black art'
    > of shortening Google Usenet URL's recently and
    > have become quite good at it. ..Maybe I should
    > write a page on it.


    Yes, please do. This is impressive.
    Ivo
     
    Ivo, Jun 13, 2004
    #3
  4. torch wrote:
    > I have trawled the internet for a solution to this one . All I need is the
    > caret position as a number. I have seen quite a few text insertion functions
    > that do something like this :
    >
    > textarea.caretPos = document.selection.createRange().duplicate()
    >
    > and then operate on the caretPos object - but all these solutions seem to
    > work by creating a selected area in the text and then replacing it - I just
    > want the caret position as a number.
    >
    > Anyone got any clues?


    In the IE DOM, TextRange objects have a boundingLeft property.
    Reads to me as it was what you are looking for:
    <http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/duplicate.asp>

    In the Gecko DOM, HTMLTextAreaElement objects have a selectionStart
    property:
    <http://www.xulplanet.com/references/objref/HTMLTextAreaElement.html>


    HTH

    PointedEars
     
    Thomas 'PointedEars' Lahn, Jun 20, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vasu
    Replies:
    0
    Views:
    113
  2. R. Kangas
    Replies:
    0
    Views:
    179
    R. Kangas
    Jan 26, 2006
  3. Replies:
    1
    Views:
    123
  4. Álvaro G. Vicario

    Limit <textarea> length without losing caret position

    Álvaro G. Vicario, May 9, 2008, in forum: Javascript
    Replies:
    7
    Views:
    171
    Thomas 'PointedEars' Lahn
    May 20, 2008
  5. ReGenesis0
    Replies:
    3
    Views:
    383
    ReGenesis0
    Sep 28, 2008
Loading...

Share This Page