cursor position in text box

Discussion in 'Javascript' started by AP, Apr 9, 2004.

  1. AP

    AP Guest

    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
     
    AP, Apr 9, 2004
    #1
    1. Advertising

  2. 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>
     
    Yann-Erwan Perio, Apr 9, 2004
    #2
    1. Advertising

  3. AP

    Ivo Guest

    "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
     
    Ivo, Apr 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaveR
    Replies:
    2
    Views:
    6,602
    Dave Rudlin
    Nov 17, 2004
  2. Alex9968
    Replies:
    0
    Views:
    582
    Alex9968
    Mar 19, 2008
  3. dmaziuk
    Replies:
    3
    Views:
    581
    Chris Gonnerman
    Jan 25, 2011
  4. Thriving K.
    Replies:
    1
    Views:
    133
    Thriving K.
    Jul 14, 2009
  5. Michael
    Replies:
    2
    Views:
    254
    Michael
    Feb 17, 2006
Loading...

Share This Page