By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,627 Members | 1,870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,627 IT Pros & Developers. It's quick & easy.

Caret Position in a Text Field

P: n/a
Hi all,

I would like to determine where the current insertion point is in a text field.

Say I had a text field that held 80 characters. I would like to display to the
user the current position of the insertion point at any point in the field.

I have found the TextRange object and have found examples of how to insert text
at a particular location. I have not been able to find a way to return a
number depicting the current location of the cursor.

Any help with this would end much frustration. Thanks in advance.

JR
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ivo
"Bambam6235" <ba********@aol.com> wrote in message
news:20***************************@mb-m29.aol.com...
Hi all,

I would like to determine where the current insertion point is in a text field.
I have found the TextRange object and have found examples of how to insert text at a particular location. I have not been able to find a way to return a
number depicting the current location of the cursor.


function caretPos(){
var i=document.f.txt.value.length+1;
if (document.f.txt.createTextRange){
theCaret = document.selection.createRange().duplicate();
while ( theCaret.parentElement() == document.f.txt
&& theCaret.move("character",1)==1 ) --i;
}
return i==document.f.txt.value.length+1?-1:i;
}

// assuming document.f.txt the reference to a textarea with name=txt
void('HTH')
Jul 20 '05 #2

P: 2
There's an excellent post here entitled "Getting/Setting the Selected Text (Caret Positions) within a Text Box in Internet Explorer and Firefox" that explains everything regarding caret positions in both browsers.

http://www.theblueform.com/Home/TheMakingOf.aspx

Pretty cool AJAX site too, they've done lots of stuff worth checking out.
Jul 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.