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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    Ipswich
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cursor positioning within a text area

    Could anyone tell me if and how it is possilbe to return the character position within a <textarea> When either moving the arrow keys, and clicking anywhere within the textarea with the mouse?

    Cheers Pete

    Here is the code I have currently, this just returns the currentcharacter position as you type.

    NOTE: the submit button doesn't do anything. It is just there for layout purposes at the moment

    -------------------------------------------------

    <BODY>
    <form method="POST">
    <table border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="100%" colspan="2">
    <textarea rows="12" name="charcount" onkeyup="return countit(this)" cols="60" wrap="virtual"></textarea>
    </td>
    </tr>
    <tr>
    <td valign="top" width="35%">Current Character Position:</td>
    <td><div id="displaycount" size="20">&nbsp;&nbsp;</div></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td align=left><input type="submit" name="submitMsg" value="Submit"></td>
    </tr>
    </table>

    </form>
    <script language="JavaScript">

    function countit(what){
    formcontent=what.form.charcount.value
    displaycount.innerHTML=formcontent.length
    }
    </script>
    </BODY>

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    In Mozilla, it is as easy as event.rangeOffset to get the caret/selection offset (event is the argument passed to your event handlers).

    IE requires some ugly TextRange work that I don't have handy.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In ie you can use:

    Code:
    function docjslib_getRealTop(imgElem) {
    	yPos = eval(imgElem).offsetTop;
    	tempEl = eval(imgElem).offsetParent;
    	while (tempEl != null) {
      		yPos += tempEl.offsetTop;
      		tempEl = tempEl.offsetParent;
      	}
    	return yPos;
    }
    to find the absolute position of an object. Just find the cursor position and subtract the abs pos.

    In case you don't know how to get the cursor position in ie:

    Code:
    <script>
    function mmove() {
    xpos = event.clientX+document.body.scrollLeft;
    ypos = event.clientY+document.body.scrollTop;
    window.status = xpos+" - "+ypos;
    }
    document.onmousemove=mmove;
    </script>
    Last edited by x_goose_x; 08-23-2002 at 05:37 PM.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    x_goose_x, it seems to me they wanted caret offset in a textarea? (maybe I misread...)

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry. I'm out to lunch


  •  

    Posting Permissions

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