How to get cursor insertion point (caret position) from HTML Textarea - Javascript

This is a discussion on How to get cursor insertion point (caret position) from HTML Textarea - Javascript ; I have previously posted this question on the "dotnet.framework.aspnet" group but that was probably a mistake because I think client-side script is required. Can anybody on this group help me to a)get the user's cursor insertion point (caret) from an ...

+ Reply to Thread
Results 1 to 3 of 3

How to get cursor insertion point (caret position) from HTML Textarea

  1. Default How to get cursor insertion point (caret position) from HTML Textarea

    I have previously posted this question on the
    "dotnet.framework.aspnet" group but that was probably a mistake
    because I think client-side script is required. Can anybody on this
    group help me to a)get the user's cursor insertion point (caret) from
    an HTML Textarea, using client-side script I suppose, and then b)add
    the caret position to a hidden HTML Input?

    I have tried the following client-side function which seems to be
    widely quoted on the web but I'm not sure what it's doing or how to
    get the caret position into the hidden HTML input("txtReading" is the
    Textarea ID from which the caret position must be collected and
    "txtcaretpos" is the hidden Input ID which must hold the caret
    position). The function is triggered as "onclick" when the user clicks
    in the Textarea "txtReading":

    function getcaretpos(txtReading)
    {debugger;
    if (txtReading.createTextRange)
    txtReading.caretPos =
    document.selection.createRange().duplicate();
    document.Form1.txtcaretpos.value = txtReading.caretPos.text
    }


    <TEXTAREA id="txtReading" title="txtReading" style="Z-INDEX: 124;
    LEFT: 521px; WIDTH: 254px; POSITION: absolute; TOP: 485px; HEIGHT:
    116px" rows="7" cols="29" runat=server
    onclick="getcaretpos(txtReading);"></TEXTAREA>

  2. Default Re: How to get cursor insertion point (caret position) from HTML Textarea

    First, do not apoint identical names to TEXTAREA ID and JavaScript function
    operator - txtReading.
    After that try this
    <textarea onClick="
    for(j=0;j<this.value.length;j++){
    if(this.value.split('\n')[j]){
    document.forms[YourForm].Elements[YourHiddenInputElementID].value+='\n'
    }
    }
    "></textarea>

    here is \n - the caret position.



  3. Default Re: How to get cursor insertion point (caret position) from HTML Textarea

    Maxim,
    Many thanks. I have tried your suggestion but get the following error
    message: "Microsoft JScript runtime error: 'this.value.length' is null
    or not an object" - any idea why?
    Here's what I have:

    function getcaretpos()
    {
    for(j=0;j<this.value.length;j++)
    {
    if(this.value.split('\n')[j])
    {
    document.forms[Form1].Elements[txtcaretpos].value+='\n'
    }
    }
    }


    <TEXTAREA id="txtReading" title="txtReading" style="Z-INDEX: 124; LEFT:
    579px; WIDTH: 254px; FONT-FAMILY: Arial; POSITION: absolute; TOP:
    315px; HEIGHT: 128px" onclick="getcaretpos()" rows="8" cols="29"
    runat="server"></TEXTAREA>


+ Reply to Thread

Similar Threads

  1. 8.5b3: bad elided text / insertion cursor interaction
    By Application Development in forum TCL
    Replies: 3
    Last Post: 12-05-2007, 09:08 AM
  2. using insertion point of data with MSWC.Tools
    By Application Development in forum Inetserver
    Replies: 0
    Last Post: 07-13-2007, 10:58 AM
  3. example code for using insertion point of data
    By Application Development in forum Inetserver
    Replies: 0
    Last Post: 06-29-2007, 01:18 PM
  4. how to get the word at the current caret position
    By Application Development in forum Adobe Indesign
    Replies: 1
    Last Post: 08-21-2006, 09:27 AM
  5. Re: html in textarea
    By Application Development in forum Java
    Replies: 0
    Last Post: 05-24-2004, 06:43 AM