So here's the answer to my own question:
<html>
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function doGetCaretPosition (ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
ctrl.focus ();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;
return (CaretPos);
}
function setCaretPosition(ctrl, pos)
{
if(ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
function process()
{
var no = document.getElementById('no').value;
setCaretPosition(document.getElementById('get'),no);
}
</script>
</head>
<body>
<textarea id="get" name="get" rows="5" cols="31">Please write some
integer
in the textbox given below and press "Set Position" button. Press "Get
Position" button to get the position of cursor.</textarea>
<br>
Enter Caret Position: <input type="text" id="no" size="1" /><input
type="button" onclick="process();" value="Set Position">
<BR>
<input type="button"
onclick="alert(doGetCaretPosition(document.getElementById('get')));"
value="Get Position">
</body>
</html>
Voila
Evertjan. said:
Simon wrote on 10 jul 2007 in comp.lang.javascript:
"Evertjan." <
[email protected]> schreef in bericht
Simon wrote on 10 jul 2007 in comp.lang.javascript:
How can I recognize the position of the cursos in a textarea?
Since 2000 this has been discussed over a 100 times:
<
http://groups.google.com/groups/search?
q=textarea+cursor.position+group:comp.lang.javascript>
[Please do not toppost on usenet]
So, give me the answer than.
Simon, this NG is not a paid helpdesk.
We, at least most of us, expect you to read past answers,
before asking the same in this NG.
It should be about 2 lines of code, in stead of 200.
How do you know?
(var pos=text.focus.position something like that? Isn't it build in?