Caret Position in a Text Field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bambam6235

    Caret Position in a Text Field

    Hi all,

    I would like to determine where the current insertion point is in a text field.

    Say I had a text field that held 80 characters. I would like to display to the
    user the current position of the insertion point at any point in the field.

    I have found the TextRange object and have found examples of how to insert text
    at a particular location. I have not been able to find a way to return a
    number depicting the current location of the cursor.

    Any help with this would end much frustration. Thanks in advance.

    JR
  • Ivo

    #2
    Re: Caret Position in a Text Field

    "Bambam6235 " <bambam6235@aol .com> wrote in message
    news:2003092318 5322.01706.0000 0100@mb-m29.aol.com...[color=blue]
    > Hi all,
    >
    > I would like to determine where the current insertion point is in a text[/color]
    field.[color=blue]
    >
    > I have found the TextRange object and have found examples of how to insert[/color]
    text[color=blue]
    > at a particular location. I have not been able to find a way to return a
    > number depicting the current location of the cursor.
    >[/color]

    function caretPos(){
    var i=document.f.tx t.value.length+ 1;
    if (document.f.txt .createTextRang e){
    theCaret = document.select ion.createRange ().duplicate();
    while ( theCaret.parent Element() == document.f.txt
    && theCaret.move(" character",1)== 1 ) --i;
    }
    return i==document.f.t xt.value.length +1?-1:i;
    }

    // assuming document.f.txt the reference to a textarea with name=txt
    void('HTH')


    Comment

    • AJAXAvatar
      New Member
      • Jul 2006
      • 2

      #3
      There's an excellent post here entitled "Getting/Setting the Selected Text (Caret Positions) within a Text Box in Internet Explorer and Firefox" that explains everything regarding caret positions in both browsers.



      Pretty cool AJAX site too, they've done lots of stuff worth checking out.

      Comment

      Working...