PDA

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



shachi
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??

Thanks.

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

ItsMeOnly
09-08-2006, 07:38 PM
Found this thing
http://www.faqts.com/knowledge_base/view.phtml/aid/1052

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

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

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



<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.