Cursor Position in Textarea

How can I capture the cursor position in a textarea form field?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

xabiConnect With a Mentor Commented:
Try this trick, Is the only thing you can do. I wrote this script for other question.

----------- 8< --------- 8< -------
<script language="javascript">
var sValue = "" 
function testit(idForm) {
 sTmp = idForm.txtmsg.value
 iXPos = 1
 iYPos = 1
 iTmp  = 0
 bIn = true
 while (iTmp<sTmp.length) {
  cChar1 = sTmp.charAt(iTmp)
  cChar2 = sValue.charAt(iTmp)
  if (cChar1 == "\r") {
   iXPos = 1
  if (cChar1 != cChar2) break
 idForm.valuex.value = iXPos
 idForm.valuey.value = iYPos
 sValue = sTmp
<body background="bgcolor.gif" vlink="orange" alink="Lime" onload="testit(document.myform)">
<form name="myform">
 <textarea name="txtmsg" rows="4" cols="27" wrap="off" onKeyUp="testit(this.form)"></textarea>
 X: <input type="text" name="valuex" readonly>
 Y: <input type="text" name="valuey" readonly>
----------- 8< --------- 8< --------

what do you mean?
If i'm correct, you want to get the exact x and y coordinates of the cursor. Not for the page but for the textarea, right?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

I think u want to capture the position of CARET (the flashing vertical bar) of the text area ...
-- Holy Spirit
At least "java-script:"
will cause errors.
Use "javascript:" instead.

Also URL where we can check all in action will be quite helpfull...
Oppsss, I posted comment in wrong thread, sorry, messed up windows...

Do you need to find cursor offset in chars from text begining inside textarea ?
This is absolutely impossible for NN4
and after spending 15 minutes in MSDN library and didn't find anything for MIE I almost sure this is also impossible for MIE too...

What can be done is replacng textarea with JavaApplet with java.lang.TextArea
inside and using this applet instead of HTML TextArea element, but this will be a lot of headaches
1. Creating applet
2. Providing methods to get/set in Applet and get Position
3. Communicating with Applet from JavaScript (Syncronization problems, as applets loads slow, than you need to be sure it loaded, before trying to access aplet methods..)
4. On submit you would need to get data from applet and copy into hidden field in form..

This can be done, but not easy..

Virtual_Max (kollegov)

bfredettAuthor Commented:
Thanks xabi!

I found your script after posting the question.  Works great.  Now, I'd like to capture the arrow keys and update the x and y textboxes as well.  Any way to do this?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.