View Full Version : Insert text in cursor(keyboard) position.

08-18-2006, 07:38 PM
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??


09-08-2006, 06:58 PM
I've really been looking for an answer to this too...

09-08-2006, 07:38 PM
Found this thing

09-09-2006, 11:01 AM
Already found the answer thanks anyways ItsOnlyMe

09-09-2006, 02:22 PM
Shachi: Could you explain your answer? This question has been bugging me :)

09-09-2006, 02:59 PM
mburt: Wait let me search in my chat script. Here found it:

<script type="text/javascript">
function insertAtCaret (textObj, textFeildValue) {
if (textObj.createTextRange && textObj.caretPos) {
var caretPos = textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?textFeildValue + ' ' : textFeildValue;
textObj.value = textFeildValue;
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;
alert("This version of Mozilla based browser does not support setSelectionRange");
<style type="text/css">

<form name="test">
<input type="button" onclick="insertAtCaret(this.form.testinp,':)');" value="Insert it">
<input type="text" value="This is a test" name="testinp">

Works fine in FF dunno in IE.