Determine charat position in text area

Discussion in 'Javascript' started by jen_designs@hotmail.com, Jun 21, 2005.

  1. Guest

    I found the following script that inserts a string into a textarea.
    What I need to find is the cursor position returned as a number. For
    instance if I have a string ABCD and the cursor is between A and B, the
    postion would be 1. Any suggestions?

    <html>
    <head>
    <SCRIPT>
    function storeCaret(textEl)
    {
    textEl.caretPos = document.selection.createRange().duplicate();
    }

    function insertAtCaret(textEl, text)
    {
    if (textEl.createTextRange && textEl.caretPos)
    {
    var caretPos = textEl.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1)
    == ' ' ? text + ' ' : text;
    }
    else
    {
    textEl.value = text;
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="aForm">
    <TEXTAREA NAME="Content" ROWS="10" COLS="80" WRAP="soft"
    ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);"
    ONKEYUP="storeCaret(this);"></TEXTAREA>
    <BR>
    <INPUT TYPE="text" NAME="bold" SIZE="80" VALUE="">
    <INPUT TYPE="button" VALUE="insert"
    ONCLICK="insertAtCaret(this.form.Content, this.form.bold.value);">
    </FORM>
    </body>
    </html>
    , Jun 21, 2005
    #1
    1. Advertising

  2. wrote:

    > I found the following script that inserts a string into a textarea.
    > What I need to find is the cursor position returned as a number. For
    > instance if I have a string ABCD and the cursor is between A and B,
    > the postion would be 1. Any suggestions?


    STFW.

    > [Invalid HTML snipped -- see <http://validator.w3.org/> for details]



    PointedEars
    Thomas 'PointedEars' Lahn, Jul 10, 2005
    #2
    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. Replies:
    1
    Views:
    3,259
    Chris Smith
    Dec 27, 2005
  2. Replies:
    11
    Views:
    604
  3. Charat Control (date Vs date)

    , Nov 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    105
  4. leo
    Replies:
    1
    Views:
    261
    Bob Lehmann
    Dec 5, 2005
  5. PeP
    Replies:
    3
    Views:
    156
Loading...

Share This Page