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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help - Cursor Jumps to end of sentence in Textarea after insertion

    I need a fix for this JS, the problem is that:

    The cursor Jumps to end of sentence in Textarea after insertion.

    When the cursor is inserted between a line of charters in the Textarea to add a character, and the Virtual Keys/Buttons (not hardware kb) are used to add text, the cursor jumps to the end of the sentence and adds the text, it does not add it where the cursor was inserted.

    I have read where this is an IE phenomenon, where IE default to the ‘end of the string f characters in the Textarea.

    Can the code below be modified / fixed?


    CODE:

    <html>
    <head>
    <script>
    fN = 0; //form name or number; 0=first form, alternatively use form name
    eN = "txt"; //text box name
    function addChar(cH) {
    document.forms[fN].elements[eN].value += cH;
    }
    function backSpace() {
    var currVal = document.forms[fN].elements[eN].value;
    document.forms[fN].elements[eN].value = currVal.substr(0,(currVal.length-1));
    }
    function getKeyFromFile(src)
    {
    var splits = src.split('/');
    temp = splits[splits.length - 1];
    temp = temp.replace('.gif','');
    temp = temp.replace('kb_','');
    return temp;
    }
    </script>
    </head>

    <body>
    <form>

    <input type="button" value="1" onclick="addChar(this.value)">

    <input type="button" value="A" onclick="addChar(this.value)">

    <input type="button" value="Space" onclick="addChar(' ')">
    <input type="button" value="&lt;--" onclick="backSpace()">
    <input type="button" value="Enter" onclick="addChar('\r\n')">&nbsp;
    <br>
    <textarea name="txt" cols="30" rows="10"> </textarea>

    </form>
    </body>
    </html>

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi! I have read your question, though not your code, but this might do it:
    (The code is not from me, and I forgot where it's from.)

    function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + myValue
    + myField.value.substring(endPos, myField.value.length);
    } else {
    myField.value += myValue;
    }
    }
    function addTextToTextarea() {
    insertAtCursor(document.getElementById("IDofTextarea"), '"Text to add");
    }

    Greetings
    Dieter
    Last edited by DH2006; 06-13-2006 at 08:59 PM.


  •  

    Posting Permissions

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