Determine charat position in text area

J

jen_designs

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>
 
T

Thomas 'PointedEars' Lahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top