473,394 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Caret position calculated incorrectly in textarea.

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>

May 7 '06 #1
1 2359
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>

May 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Zbigniew Braniecki | last post by:
Hello. I'm looking for any way to grab caret position in textarea (start, end - if selection) in Internet Explorer. Is it at all possible? Also any idea for doing it in Opera, KHTML would be...
4
by: torch | last post by:
I have trawled the internet for a solution to this one . All I need is the caret position as a number. I have seen quite a few text insertion functions that do something like this : ...
7
by: Paul Gorodyansky | last post by:
Hi, Say I have a text in my TEXTAREA box - 01234567890 I want - using script - insert say "abc" in the middle. Works almost OK in Internet Explorer (with one problem) based on their example...
2
by: rjonasz | last post by:
Hey Everyone, I'm trying to scroll a textarea to the position of the caret which is below the visible area of the textarea in firefox. I have code which works in IE but the code I use for...
7
by: =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
I need to emulate the missing "maxlegth" attribute in "textarea" fields but all my Google searches either lead to obsolete scripts that overwrite the "value" property (thus losing caret position)...
3
by: ReGenesis0 | last post by:
Is it possible to determine the caret position, in terms of x/y pixels, within a textarea? (I want to have a suggestion box pop up under where you're typing... so i need to determine where you are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.