Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > cursor position in text box

Reply
Thread Tools

cursor position in text box

 
 
AP
Guest
Posts: n/a
 
      04-09-2004
I am trying to find the integer value for the cursor position in a text box,
in particular I want to find out if I am at the end of some text that I
typed or at the beginning.

I am using IE 6.

Thanks,

Andy


 
Reply With Quote
 
 
 
 
Yann-Erwan Perio
Guest
Posts: n/a
 
      04-09-2004
AP wrote:

> I am trying to find the integer value for the cursor position in a text box,
> in particular I want to find out if I am at the end of some text that I
> typed or at the beginning.


The following will give you the position of the caret, the rest is a
matter of substring-ing the control value and determine whether the
position is the one required.

Few browsers support ranges, though; you can expect it to work on IE and
Mozilla, but probably no other agents yet. Limitations in supporting
browsers are to be sought in the function triggering (can you see
situations where the function won't be triggered while it should be?).


<form>
<input type="text" onkeyup="s(this)" onclick="s(this)">
<input type="text" name="position">
</form>

<script type="text/javascript">
function s(el){
var sel, rng, r2, i=-1;

if(typeof el.selectionStart=="number") {
i=el.selectionStart;
} else if(document.selection && el.createTextRange) {
sel=document.selection;
if(sel){
r2=sel.createRange();
rng=el.createTextRange();
rng.setEndPoint("EndToStart", r2);
i=rng.text.length;
}
} else {
el.onkeyup=null;
el.onclick=null;
}

el.form.elements["position"].value=i;
}
</script>
 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      04-09-2004
"AP" wrote
> I am trying to find the integer value for the cursor position in a text

box,
> in particular I want to find out if I am at the end of some text that I
> typed or at the beginning.
>
> I am using IE 6.


That 's fine, but the question is what your users use. Incidentally, the
following code is 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;
}
// assuming document.f.txt is your textbox.

If the cursor is not in the textbox, the function returns a value of -1,
similar to the indexOf() method for strings.
HTH
Ivo


 
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
how to tell if cursor is sqlite.Cursor or psycopg2.Cursor dmaziuk Python 3 01-25-2011 04:52 AM
How to check position of cursor blinking on text box Thriving K. Ruby 1 07-14-2009 04:22 AM
Tkinter.Text widget - how to get text cursor position? Alex9968 Python 0 03-19-2008 05:37 PM
Finding the position of the cursor in a text box & Rich formated <textarea> Michael Javascript 2 02-17-2006 02:33 PM
How to get cursor insertion point (caret position) from text box DaveR ASP .Net 2 11-17-2004 09:39 AM



Advertisments