Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post

    how do you get cursor position?

    Hi, I forget how you get the cursor position in a text field.
    Could somebody tell me how you do this please?
    Kris Hubby
    kwhubby site

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do not know if this is what you are looking for.
    But, it may help
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function storeCaret(ftext){
    if (ftext.createTextRange){
    ftext.caretPos = document.selection.createRange().duplicate();
    }
    }
    
    function insertsmilie(smilieface){
    var t = document.f.t;
    if (t.createTextRange && t.caretPos){
    var caretPos = t.caretPos;
    caretPos.text = smilieface;
    t.focus();
    }
    else{
    t.value+=smilieface;
    t.focus();
    }
    }
    </script>
    <body onload="document.f.t.focus()">
    <form name=f>
    <textarea name=t cols="44" rows="15" onselect="storeCaret(this);"onclick="storeCaret(this);"
    onkeyup="storeCaret(this);" onchange="storeCaret(this);"></textarea><br>
    <button onclick="insertsmilie('8)');">Insert Smilie</button><br>
    </form>
    </body>
    </html>
    .....Willy

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    thanks!, that is sortov what I am looking for!, although it does not work properly in netscape: one reason being that its using <button> instead of <input type="button">, and the other Is that in netscape it doesnt "insert" it simply adds at the end.

    Although what i am doing is primarily for IE, id like to know how to do it for netscape 6+
    Kris Hubby
    kwhubby site

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    function insert(text, textarea) {
    textarea.value = textarea.value.substr(0, textarea.selectionStart) + text + textarea.value.substr(text.length+textarea.selectionStart);
    }

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    sorry for the delay between for my reply. I tried what u said to do jkd but It inserts the text in the beggining of the textarea- I make it alert textarea.selectionStart tells me that it is undifined. Whats going on here, how do i get the cursor position for a textarea in moz?
    Kris Hubby
    kwhubby site


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •