HighDots Forums  

Re: get a a char's position when its string is clicked

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: get a a char's position when its string is clicked in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ivo
 
Posts: n/a

Default Re: get a a char's position when its string is clicked - 09-21-2003 , 02:55 AM







"charly" <kanari667 (AT) yahoo (DOT) fr> wrote

Quote:
Is it possible to get the cursor's position in an input text?
Try this function (IE only):

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;
}

You 're lucky you have an input. I use this function in a textarea, and if
my cursor is at the 345th position, it takes quite a while for the while
loop on line 5 to reach the end.
HTH
Ivo





Reply With Quote
  #2  
Old   
charly
 
Posts: n/a

Default Re: get a a char's position when its string is clicked - 09-22-2003 , 02:36 AM






Works great with Ie. Didn't know about the createTextRange,
parentElement stuff

Thank you very much !


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.