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