Results 1 to 6 of 6

Thread: Insert text in cursor(keyboard) position.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Insert text in cursor(keyboard) position.

    Can anyone tell me how do I insert text where the cursor of the keyboard is??

    Let me explain my situation.

    First I have a simple text box and it has the following content:

    This is a test.

    Now he wants to add some smileys in between and the cursor is somewhere here:

    This is |a test.

    Where the | is the keyboard cursor. Now how do I insert the smiley exactly where the cursor is??

    This is |a test.

    I need something like this forums replying system where the smileys are inserted where the cursor is.

    I hope I was able to describe my situation.

    Any ideas??

    Thanks.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I've really been looking for an answer to this too...
    - Mike

  3. #3
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Already found the answer thanks anyways ItsOnlyMe

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Shachi: Could you explain your answer? This question has been bugging me
    - Mike

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mburt: Wait let me search in my chat script. Here found it:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function insertAtCaret (textObj, textFeildValue) {
    if(document.all){
    if (textObj.createTextRange && textObj.caretPos) {
    var caretPos = textObj.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?textFeildValue + ' ' : textFeildValue;
    }else{
    textObj.value = textFeildValue;
    }
    }else{
    if(textObj.setSelectionRange){
    var rangeStart = textObj.selectionStart;
    var rangeEnd = textObj.selectionEnd;
    var tempStr1 = textObj.value.substring(0,rangeStart);
    var tempStr2 = textObj.value.substring(rangeEnd);
    textObj.value = tempStr1 + textFeildValue + tempStr2;
    }else{
    alert("This version of Mozilla based browser does not support setSelectionRange");
    }
    }
    }
    </script>
    <style type="text/css">
    
    </style>
    </head>
    <body>
    <form name="test">
    <input type="button" onclick="insertAtCaret(this.form.testinp,':)');" value="Insert it">
    <input type="text" value="This is a test" name="testinp">
    </form>
    </body>
    </html>
    Works fine in FF dunno in IE.

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
  •