SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change cursor position in Textarea?

    Hi all,
    Does anyone know how to change the position of the caret (cursor) in a Textarea, using Javascript?

    I've stored the caret position using this (thanks FAQTS!):-

    Code:
    function storeCaret(textEl) {
    	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
    }
    How would I move the cursor back 3 spaces, for example? Is it easy to do?

    Thanks,

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uh, can you elaborate on this one a little bit? i have some ideas but i'm still unclear as to what you're doing.

  3. #3
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replying makeda, of course I will elaborate...

    I'm writing some basic forum software which parses UBBCode, much as SitePointForums does. When a user clocks on the "Italics" link, the following code is inserted at the cursor position....

    Code:
    (i) (/i)
    ...and the cursor ends up AFTER the closing tag.

    I think it would be better for the user if the cursor ended up inbetween the tags. Therefore I have to move the cursor back 4 positions (not 3, as I said before = OOPS!).

    Any ideas?

  4. #4
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My solution would be to do it more like sitepoint does. Here is a crude example but obviously, you could customize it however you like:

    Code:
    <html>
    <head>
    
    <script type="text/javascript" language="javascript">
    
    function insertTag(sMode) {
      var sInput;
    
      sInput = prompt("Please Insert Your Text","");
      frmTest.myTextArea.value += "(" + sMode + ")" + sInput + "(/" + sMode + ")";
    }
    </script>
    
    </head>
    <body>
    
    <form name="frmTest">
    <textarea name="myTextArea"></textarea>
    <br />
    <input type="button" value="italics" name="btnItal" onClick="insertTag('i')"><br />
    <input type="button" value="bold" name="btnItal" onClick="insertTag('b')">
    
    </form>
    
    </body>
    </html>


Bookmarks

Posting Permissions

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