Entry
How can I get the carret(cursor) postition in a textarea as an integer to use as an index for the textarea.value?
Oct 19th, 2001 18:56
vempati suresh, Sindri Traustason,
<html>
<head>
<script>
function expand(obj)
{
obj.rows=obj.rows+1
}
function colapse(obj)
{
arr = new Array()
x=obj.value;
i=0;
j="";
for(k=0;k<x.length; k++)
{
if( ( x.charCodeAt(k) == 13) && (x.charCodeAt(k+1) ==
10) )
{
arr[i]= j
j=""
i++
}else
{
j=j +x.charAt(k)
}
}
cnt=0
for(k=x.length-1;k>=0;k--)
{
if(x.charCodeAt(k)!=10 && x.charCodeAt(k)!=13)
break;
}
for(m=k;m<x.length;m++)
{
if(x.charCodeAt(m)==10)
cnt++
}
obj.value=x.substring(0,k+1)
obj.rows=arr.length-cnt+1
}
function deleteRows(obj)
{
if( (event.keyCode==46) || (event.keyCode==8) )
{
t=0;
if (obj.createTextRange)
{
obj.caretPos = document.selection.createRange
().duplicate();
obj.caretPos.text = String.fromCharCode(46)
actValue=obj.value.replace(String.fromCharCode
(46),'')
val = obj.value
for(p=0;p<val.length;p++)
{
if(val.charCodeAt(p)==10)
t++
if(val.charAt(p)!=actValue.charAt(p))
break;
}
}
colapse(obj)
if (obj.createTextRange && obj.caretPos)
{
var r = obj.createTextRange();
obj.value = obj.value.replace(String.fromCharCode
(46),'')
r.moveStart('character',parseInt(p)-parseInt(t))
r.collapse();
r.select();
}
}
else if ( event.keyCode==13)
expand(obj)
}
</script>
</head>
<body>
<form name="generalForm">
<textarea cols="70" rows="1" style="overflow:hidden;border:0"
name="GeneralNotes" onKeyUp="deleteRows(this)" onBlur="colapse
(this)"></textarea>
</body>
</html>