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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can i set the (caret??) mouse pointer in the end of string

    Hello
    i inserting simple string into textarea (whit js txt.value="blah" ) now i like to to see the
    caret (im not sure if its name is caret , but i mean the blinking vertical line that blinks when we type in textarea)
    in the end of the string i entered , what i tried to do is to put right after the txt.value="blah"
    txt.focus();
    it do give me that "blinking line" but only at the begging of the string.
    so my question is how to move it to the end of the string.
    thanks

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Take a look at this post:
    text cursor on a textarea

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    var ta = document.forms[0].elements['someTextArea'];
    var r = ta.createTextRange();
    r.expand( "textedit" );
    r.collapse( false );
    r.select();
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    This worked a while ago


    txt.value="blah"
    txt.focus();
    txt.value+=""

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Posts
    143
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all to the fast replies

    now im facing new problem say i have the string "foo1 foo2" in the textarea
    now i like to insert new string between "foo1" and "foo2" say it will be "foo3" so it will look like "foo1 foo3 foo2"
    how can i keep the "blinking vertical line" on the end of this new string ("foo3") ?
    thanks

  • #6
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,
    Is this what you're looking for?

    PHP Code:
    <html>
    <
    head>
    <
    script language='javascript' type='text/javascript'
    function 
    storeCaret (txtObj)
    {
      if (
    txtObj.createTextRange
        
    txtObj.cPos document.selection.createRange().duplicate();
    }

    function 
    insertAtCaret (txtObjtext)
    {
      if (
    txtObj.createTextRange && txtObj.cPos)
      {
        var 
    cPos txtObj.cPos;
        
    cPos.text =
          
    cPos.text.charAt(cPos.text.length 1) == ' ' ?
                 
    text ' ' text;
      }
      else
        
    txtObj.value  text;

      
    txtObj.cPos.select();
    }
    </script>
    </head>
    <body>
    <form name="a">
    <textarea name="b" wrap="soft" rows="5" cols='30' onselect="storeCaret(this);" onclick = "storeCaret(this);" onkeyup="storeCaret(this);">
    foo1 foo2
    </textarea>
    <br>
    <input type="text" name="c" value="foo3">
    <br>
    <input type="button" value="insert at caret"
    onclick="insertAtCaret(this.form.b, this.form.c.value);">
    </form>
    </body>
    </html> 
    Vinny
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Posting Permissions

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