PDA

View Full Version : Cursor Location within form elements


tEE
04-30-2003, 08:01 PM
is it possible to detect which form element the cursor is currently in, and where in that element it is?

DarkStreetDev
05-02-2003, 11:45 PM
Originally posted by tEE
is it possible to detect which form element the cursor is currently in, and where in that element it is?

As far as I know, you can't know where in the element the cursor is in. As to your fisrt question: MAYBE you can use the focus() to see where it is...

Maybe you should rather tell us what you want to do, maybe we can find a better or easier sollution.

Jon Hanlon
05-03-2003, 10:30 PM
It's not easy.

Microsoft have a way of finding the caret at http://msdn.microsoft.com/library/en-us/dnwebteam/html/webteam12032001.asp?frame=true


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

tEE
05-04-2003, 05:09 PM
I'm building a content management system for my company and don't want the user to have to type <br> to make line breaks

tEE
05-06-2003, 07:54 PM
That script did it, thanks for that :)