Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Caret position calculated incorrectly in textarea.

Reply
Thread Tools

Caret position calculated incorrectly in textarea.

 
 
j_macaroni@yahoo.com
Guest
Posts: n/a
 
      05-07-2006
I found this code to calculate the caret position in a textarea. In the
script getCaret below, I pass it the TextArea1 element.
It seems to work until you press a CR. In which case you get the wrong
result because of this line: "textEl.caretPos.move("character",1)==1 )
--i; "

So now if you have 3 lines and you are at line 0, column 0 it shows a
5. I believe the move() method skips over CR/LF but
"textEl.value.length+1;" includes the CR/LF.

I know it has to do with CR/LF. Can anyone suggest how to fix this?
Thanks.

<html>
<body>
<script language=javascript>
function getCaret (textEl) {
if (textEl.createTextRange) {
textEl.caretPos = document.selection.createRange().duplicate();
var i=textEl.value.length+1;
while ( textEl.caretPos.parentElement() == textEl
&& textEl.caretPos.move("character",1)==1 ) --i;
return (i==textEl.value.length+1?-1:i);
}
}
</script>

<input name=caretPos type=text />
<TEXTAREA name="TextArea1"
style="position:absolute;left:64px;top:112px;width :816px;height:209px;z-index:2"
rows=10 cols=79
id="TEXTAREA1"
ONSELECT="document.main.caretPos.value=getCaret(th is);"
ONCLICK="document.main.caretPos.value=getCaret(thi s);"
ONKEYUP="document.main.caretPos.value=getCaret(thi s);" >
</TEXTAREA>
</body>
</html>

 
Reply With Quote
 
 
 
 
j_macaroni@yahoo.com
Guest
Posts: n/a
 
      05-07-2006
forgot the form name=main.

<form name=main>
<input name=caretPos type=text />
<TEXTAREA name="TextArea1"
style="position:absolute;left:64px;top:112px;width :816px;height:209px;z-ind*ex:2"

rows=10 cols=79
id="TEXTAREA1"
ONSELECT="document.main.caretPos.value=getCaret(th is);"
ONCLICK="document.main.caretPos.value=getCaret(thi s);"
ONKEYUP="document.main.caretPos.value=getCaret(thi s);" >
</TEXTAREA>
</form>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
advanced q?: mouseup position to caret position? brendan Javascript 0 08-29-2006 10:48 AM
How to set system.out.print caret at the same position? Camel Java 3 07-12-2005 04:55 PM
JEditorPane (text/html) - determining position of caret in HTML source Robin Java 0 05-12-2005 03:48 PM
How to get cursor insertion point (caret position) from text box DaveR ASP .Net 2 11-17-2004 09:39 AM
Current Caret Position =?Utf-8?B?SHVnaA==?= ASP .Net 1 02-05-2004 01:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57