PDA

View Full Version : Finding Cursor Location


tEE
05-01-2003, 09:43 PM
I can successfully alter text in a textfield which is highlighted, but now i want to be able to add text to the field where ever the cursor is, how can i locate its position?:cool:

DarkStreetDev
05-02-2003, 11:48 PM
I tried to do what you want, but unfortunately, regardless of where the cursor is, the text is ALWAYS added at the end of the last text. :(

tEE
05-04-2003, 05:05 PM
Ow that sucks, i'll give u a msg if i find some magic way to do it

tEE
05-06-2003, 07:56 PM
this script works perfectly :)

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JScript">
function saveCaret(elem)
{
if ( elem.isTextEdit )
elem.caretPos = document.selection.createRange();
}
function getCaretPos(elem)
{
if ( elem.isTextEdit && elem.caretPos )
{
var bookmark = "~";
var orig = elem.value;
var caretPos = elem.caretPos;
caretPos.text = bookmark;
var i = elem.value.search( bookmark );
window.status = "Caret is at character " + i;
elem.value = orig;
}
}
</SCRIPT>
</HEAD>

<BODY>
<INPUT NAME="txtInput" ONSELECT="saveCaret(this)"
ONCLICK="saveCaret(this)" ONKEYUP="saveCaret(this)" VALUE="Where are you?">
<INPUT TYPE="button" VALUE="caret pos" ONCLICK="getCaretPos(txtInput)">
</BODY>
</HTML>